How to copy text to clipboard/pasteboard with Swift
Asked Answered
L

6

263

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.

Libna answered 10/7, 2014 at 6:58 Comment(1)
Objective-C: hayageek.com/uipasteboard-example-read-write-shareAlgeciras
B
638

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.)

Broiler answered 10/7, 2014 at 7:40 Comment(3)
This may be working fine, but doesn't it happen that the user has images or files in it's copied content ?Ikkela
UIPasteboard.general.string works in swift 4.x tooMelody
How can I copy font with font style?Cornwall
J
71

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.

Jackscrew answered 8/6, 2016 at 1:50 Comment(5)
How can i copy text with font style. So i can use that font StyleCornwall
@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.Jackscrew
okay what If i copy text from my App and paste into WhatsApp or Any Other App Dose it support style?Cornwall
@KrunalNagvadia, No, the iOS system UIPasteboard does not support styling. You can only handle styling within your own app.Jackscrew
According to Apple Docs: "Do not use UIPasteboard.general.string to determine if a pasteboard contains string data. Instead, use the hasStrings property."Nyctalopia
F
11

Copying text from the app to the clipboard:

let pasteboard = UIPasteboard.general
pasteboard.string = employee.phoneNumber
Ferdie answered 18/4, 2017 at 11:5 Comment(0)
A
11

SWIFT 4

UIPasteboard.general.string = "TEXT"
Aceto answered 29/4, 2019 at 23:43 Comment(0)
F
8

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
Frere answered 2/4, 2022 at 17:7 Comment(1)
How would this answer differ from accepted answer and other existing answers?Pinkerton
B
0
import UIKit.UIPasteboard

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

  static func readFromClipboard() -> String? {
    return self.general.string
  }
}
Berkley answered 6/2, 2023 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.