How to copy text to clipboard/pasteboard with Swift

If all you want is plain text, you can just use the string property. It’s both readable and writable:

// write to clipboard
UIPasteboard.general.string = "Hello world"

// read from clipboard
let content = UIPasteboard.general.string

(When reading from the clipboard, the UIPasteboard documentation also suggests you might want to first check hasStrings, “to avoid causing the system to needlessly attempt to fetch data before it is needed or when the data might not be present”, such as when using Handoff.)

Leave a Comment