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:

To retrieve the value:

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:

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:

and to retrieve your data

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:

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:

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.

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

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

  1. Should this work on custom keyboards? I cannot get an image into general pasteboard, hasimages always returns false?

      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 😉

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.