Swift version: 5.6
You can write to and read from the iOS clipboard by using the UIPasteboard
class, which has a generalPasteboard()
method that returns the shared system method of 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 Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
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.