UIPasteboard Example – Read, Write and Share data between apps

In UIPasteboard Example, I have explained how to read data from UIPasteboard and write data to UIPasteboard and share data between apps.
Pasteboard can be used to store Plain Text, Rich-Text, Image, Email,…etc.

Topics covered in this tutorial:
1).UIPasteboard APIs
2).Writing Data to General Pasteboard
3).Reading Data to General Pasteboard
4).Writing Data to App Pasteboard
5).Reading Data from App Pasteboard
6).Remove a Pasteboard

UIPasteboard Example Download

1.UIPasteBoard APIs

1.1 APIs for creating a Pasteboard.

+ (UIPasteboard *)generalPasteboard;
+ (UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create;
+ (UIPasteboard *)pasteboardWithUniqueName;

generalPasteboard : method returns system board which supports general copy-paste operations.
pasteboardWithUniqueName : method is used to create a app pasteboard which is identified by a unique system generated name.
pasteboardWithName: method returns a a pasteboard identified by name. if create flag is TRUE, then pasteboard is created.

Note: Data can be shared between apps using app pasteboard.

 

1.2 APIs for writing data to Pasteboard.

//setValue method is used to store objects. 
- (void)setValue:(id)value forPasteboardType:(NSString *)pasteboardType;

//setData method is used to store raw data
- (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType;

pasteboardType : is Uniform Type Identifier  Ex: kUTTypePlainText, kUTTypeText, kUTTypeJPEG, kUTTypePNG.
To get the list of system declared Uniform Type Identifiers: Click Here

 

1.3 APIs for reading data from Pasteboard.

//returns data 
- (NSData *)dataForPasteboardType:(NSString *)pasteboardType;

//returns value
- (id)valueForPasteboardType:(NSString *)pasteboardType;

2.Writing data to Generate Pasteboard

2.1 Copy Text to General Pasteboard

Text can be copied to Pasteboard using setString / setData.

//Method 1
NSString * text=@"Ravi";
UIPasteboard * pasteboard=[UIPasteboard generalPasteboard];
[pasteboard setString:text];

//Method 2
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
[pasteboard setData:data forPasteboardType:(NSString *)kUTTypeText];

2.2 Copy Image to General Pasteboard

Image can be copied to Pasteboard using setImage / setData.

//Method 1
UIImage * image=[UIImage imageWithContentsOfFile:@"FILE_PATH"];
UIPasteboard * pasteboard=[UIPasteboard generalPasteboard];
[pasteboard setImage:image];

//Method 2
NSData *imageData = UIImagePNGRepresentation(image);
[pasteboard setData:imageData forPasteboardType:(NSString *)kUTTypePNG];

3). Reading Data to General Pasteboard

Text can be read using string attribute/dataForPasteboardType.

UIPasteboard * pasteboard=[UIPasteboard generalPasteboard];
//Method 1
NSLog(@"Text =%@",[pasteboard string]);

//Method 2
NSData * data = [pasteboard dataForPasteboardType:(NSString*)kUTTypeText];
NSString * text =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Text =%@",text);

4). Writing Data to App Pasteboard

You can use below method, to write a dictionary to App pasteboard.

#define PASTE_BOARD_NAME @"mypasteboard"
#define PASTE_BOARD_TYPE @"mydata" 

-(void) writeToPasteBoard:(NSDictionary*) dict
{
    UIPasteboard * pb = [UIPasteboard pasteboardWithName:PASTE_BOARD_NAME create:YES];
    [pb setPersistent:TRUE];

    [pb setData:[NSKeyedArchiver archivedDataWithRootObject:dict] forPasteboardType:PASTE_BOARD_TYPE];
}

By default, Pasteboard created with pasteboardWithName or pasteboardWithUniqueName are not persistent. To make them persistent, set the persistent to TRUE.

NSKeyedArchiver is used to encode NSDictionary as NSData.

5). Reading Data from App Pasteboard

You can use below method, to read data from Pasteboard.

-(NSDictionary*) readFromPasteboard
{

    UIPasteboard * pb=[UIPasteboard pasteboardWithName:PASTE_BOARD_NAME create:NO];
    NSData * data=[pb valueForPasteboardType:PASTE_BOARD_TYPE];
    NSDictionary * dict;

    if (!data) {
        return nil;
    }
    @try {
        dict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    }
    @catch (NSException* exception)
    {
        NSLog(@"Exception: %@",exception);
        return nil;
    }
    return dict;
}

NSKeyedUnarchiver is used to decode NSData as NSDictionary.

6).Remove Pasteboard

Pasteboard data can be removed using method removePasteboardWithName.

[UIPasteboard removePasteboardWithName:PASTE_BOARD_NAME];