Apple provides the UIPasteboard class which enables the data to be shared within the app and with another app. To share data we can use pasteboards.
Here is the code snippet depicting how copy and Paste functionality can be achieved in Swift using UIPasteBoards.
To copy the text using pasteboard:
1- Create the pasteboard object using general or shared system pasteboard
let pasteBoard = UIPasteboard.generalPasteboard();
2- Use property ‘string’ to set the text to be copied.
pasteBoard.string = textTobeCopied; // Set your text to be copied here.
To Print or fetch the text which was copied earlier :
1- Create the pasteboard object using shared system.
let pasteBoard = UIPasteboard.generalPasteboard()
2- use extension property ‘string’ of pasteboard to get the copied string.
print(Text copied \(pasteBoard.string))
Thanks for reading
0 Comment(s)