How to Use UIPasteboard in Swift?

Make copy-paste text to Pasteboard on IOS

UIPasteboard is an object that helps users share data from one place to another place in-app, also from app to other apps. We can call it “copy-paste” shortly.

Supported types are NSString, UIImage, NSURL ve UIColor. An example for string:

<paste let string = “Kopyala Yapıştır!”
let pasteboard = UIPasteboard.generalPasteboard()
pasteboard.string = string

To retrieve the value:

let pasteboard = UIPasteboard.generalPasteboard()
let string = pasteboard.string
print(string)

It’s very easy to use as you can see. Let’s see an example from an app. You want user to copy a text from your app. Only one line code:

@IBAction func copyBtn(_ sender: Any) {UIPasteboard.general.string = detailText.text}

We copied text from DetailText with our copyBtn function. Happy coding!

--

--

Murat Menzilci

📱 I write about how to make money with mobile application development, news about App Store and Google Play Store, working as a freelancer and making money. 💰