259

I'm looking for a clean example of how to copy text to iOS clipboard that can then be used/pasted in other apps.

The benefit of this function is that the text can be copied quickly, without the standard text highlighting functions of the traditional text copying.

I am assuming that the key classes are in UIPasteboard, but can't find the relevant areas in the code example they supply.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Garry Law
  • 2,936
  • 2
  • 16
  • 14

7 Answers7

627

If all you want is plain text, you can just use the string property. It's both readable and writable:

// write to clipboard
UIPasteboard.general.string = "Hello world"

// read from clipboard
let content = UIPasteboard.general.string

(When reading from the clipboard, the UIPasteboard documentation also suggests you might want to first check hasStrings, "to avoid causing the system to needlessly attempt to fetch data before it is needed or when the data might not be present", such as when using Handoff.)

jtbandes
  • 115,675
  • 35
  • 233
  • 266
69

Since copying and pasting is usually done in pairs, this is supplemental answer to @jtbandes good, concise answer. I originally came here looking how to paste.

iOS makes this easy because the general pasteboard can be used like a variable. Just get and set UIPasteboard.general.string.

Here is an example showing both being used with a UITextField:

Copy

UIPasteboard.general.string = myTextField.text

Paste

if let myString = UIPasteboard.general.string {
    myTextField.insertText(myString)
}

Note that the pasteboard string is an Optional, so it has to be unwrapped first.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • How can i copy text with font style. So i can use that font Style – Krunal Nagvadia Nov 01 '19 at 05:15
  • @KrunalNagvadia, You can't copy text with the font style, but you can get the font info with `myTextField.font`. You can save that information separately and apply it somewhere else. – Suragch Nov 01 '19 at 06:30
  • okay what If i copy text from my App and paste into WhatsApp or Any Other App Dose it support style? – Krunal Nagvadia Nov 01 '19 at 06:38
  • @KrunalNagvadia, No, the iOS system `UIPasteboard` does not support styling. You can only handle styling within your own app. – Suragch Nov 01 '19 at 07:03
  • 3
    According to [Apple Docs](https://developer.apple.com/documentation/uikit/uipasteboard/1622092-string): "Do not use `UIPasteboard.general.string` to determine if a pasteboard contains string data. Instead, use the `hasStrings` property." – Eric33187 Apr 14 '21 at 06:34
14

in Swift 5 i can copy text to clipboard using

UIPasteboard.general.string = "Hello world"

then you can paste the text anywhere of your device

M Mahmud Hasan
  • 1,303
  • 1
  • 13
  • 20
11

Copying text from the app to the clipboard:

let pasteboard = UIPasteboard.general
pasteboard.string = employee.phoneNumber
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Raj Joshi
  • 2,669
  • 2
  • 30
  • 37
10

SWIFT 4

UIPasteboard.general.string = "TEXT"
Álvaro Agüero
  • 4,494
  • 1
  • 42
  • 39
5

Write below the code where you want to Copying String or Text

UIPasteboard.general.string = "Dhaval Gevariya" // Put your String here

this is for read String from clipboard.

var readString = UIPasteboard.general.string
Dhaval Gevariya
  • 870
  • 1
  • 12
  • 16
0
import UIKit.UIPasteboard

extension UIPasteboard {
  static func pasteToClipboard(_ content: String) {
    self.general.string = content
  }

  static func readFromClipboard() -> String? {
    return self.general.string
  }
}