How to use UIPasteboard to share data with iOS

Screen Shot 2015-02-02 at 11.56.41

UIPasteboard allows you to copy data to iOS and paste it back into your own or other apps. Think of copying text from a Pages document and pasting it into an email. That’s UIPasteboard hard at work.

The class supports NSString, UIImage, NSURL and UIColor out of the box. You can add your own items to the pasteboard like this:

NSString *myString = @"Hello";
[UIPasteboard generalPasteboard].string = myString;

To retrieve the value:

NSString *myString = [UIPasteboard generalPasteboard].string;
NSLog(@"%@", myString);

The principle is the same for the other data types.

Copying and Pasting multiple items

Single values aside, UIPasteboard also has the ability to store the above data types as an NSArray. IF you had multiple values you’d like to add, you can use this:

NSString *myString = @"Hello";
NSString *myOtherString = @"again";
NSArray *stringArray = @[myString, myOtherString];
[UIPasteboard generalPasteboard].strings = stringArray;

This works with strings, images, URLs and colors.

Binary Data

UIPasteboard can also handle binary data from an NSData object. Here’s how to set data:

NSData *myData = [NSData dataWithContentsOfFile:@"myFile"];
[UIPasteboard generalPasteboard] setData:myData forPasteboardType:@"yourUTI"];

and to retrieve your data

NSData *moreData = [[UIPasteboard generalPasteboard]dataForPasteboardType:@"yourUTI"];

The dataForPasteboardType parameter is an NSString of your choosing so that you can identify it as your data. Apple recommend to use the reverse DNS structure like com.yourdomain.yourapp.yourdata.

Creating your own Pasteboards

If you don’t want to share data with the general iOS Pasteboard, you can create your own:

UIPasteboard *myPasteboard = [UIPasteboard pasteboardWithName:@"FunkyPasteboard" create:YES];

To retrieve values from it in other classes, instantiate it again and pass NO as the create parameter.

The Find Pasteboard Myth

According to Apple’s documentation, there’s also the Find Pasteboard. Legend has it that it holds the latest string from Spotlight search. We should be able to access it like this:

UIPasteboard *findPasteboard = [UIPasteboard pasteboardWithName:UIPasteboardNameFind create:NO];
NSString *latestSearch = findPasteboard.string;
NSLog(@"%@", latestSearch);

which sadly doesn’t work. Even though the object exists and is valid, all of its properties are always nil, no matter how are we search for things. It’s therefore best to forget the Find Pasteboard exists and never mention it again.

4 thoughts on “How to use UIPasteboard to share data with iOS

      1. Ah, I got it to work!

        I was using NSData but then found I did not need to do that as can use .image directly


        THANKS 😉

          //var myData = NSData()
         // myData = NSData(data: UIImagePNGRepresentation(UIImage(named: "mymoji")!)!)
        //UIPasteboard.general.setData(myData as Data, forPasteboardType:"com.mycom.mymoji")
        UIPasteboard.general.image = UIImage(named: "mymoji")
        if (UIPasteboard.general.hasImages) {
            self.textDocumentProxy.insertText("Image in pasteboard");
         else {
          self.textDocumentProxy.insertText("no image");

Leave a Reply