Swift version: 5.6
You can write to and read from the iOS clipboard by using the UIPasteboard
class, which has a general
property that returns the shared system space for copying and pasting data between apps. Using this you can write text to the clipboard just like this:
let pasteboard = UIPasteboard.general
pasteboard.string = "Hello, world!"
To read text back from the clipboard, you should unwrap its optional value like this:
if let string = pasteboard.string {
// text was found and placed in the "string" constant
}
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 3.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.