UIPasteboard: string property
The string property is the text on the clipboard. Reading it fetches the content; writing it replaces the content.
Value
A string or null.
- Readable and writable.
- Reads
nullwhen there is no text on the clipboard. Readsnullwhen the clipboard holds only an image; not verified in the simulator. - Writing a piece of text replaces the existing content of the clipboard. Writing needs no user consent and shows no system prompt. Once the assignment line has run, the text is on the clipboard.
- Writing
nullclears the clipboard: afterwardstringreadsnullandhasStringsisfalse. It clears the whole system clipboard, including content the user copied in another app. - Every write (including writing
null) increaseschangeCountand fires thechangeevent. There is no event when another app changes the content; seechangeCount.
Note: When the content comes from another app, reading
stringshows a system prompt, and the line does not return until the user responds; the whole interface freezes. See The system prompt when reading. To know only whether there is text, readhasStrings.
Invalid values
Values that are not strings are converted with String() on write; for example, writing 42 gives '42'. Writing undefined clears it, the same as writing null. Writing never throws.
Examples
js
const pasteboard = UIPasteboard.generalPasteboard;
pasteboard.string = '20261112-0042';
console.log(pasteboard.string);
pasteboard.string = null;
console.log(pasteboard.string, pasteboard.hasStrings);Result: First prints 20261112-0042; after clearing, prints null false.
Corresponding iOS API
UIPasteboard.string.
See also
- Copying and pasting text: when to read without disturbing the user
hasStrings: checking only whether there is text