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 Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
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.