Swift version: 5.10
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 Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
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.