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
}
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.