Garry Law Garry Law - 5 months ago 132
Swift Question

How to copy text to clipboard/pasteboard with Swift

I'm a Swift newbie looking for a clean example of how to copy text to iOS clipboard that can then be used/pasted in other apps.

The benefit of this function is that the text can be copied quickly, without the standard text highlighting functions of the traditional text copying.

I am assuming that the key classes are in UIPasteboard, but can't find the relevant areas in the code example they supply: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPasteboard_Class/index.html

Any help would be very much appreciated!
Thanks,
G

Answer

If all you want is plain text, you can just use the string property:

UIPasteboard.generalPasteboard().string = "Hello world"
Comments