This post will explain you how you can share data between apps in iPhone, using copy, paste.

By defaults many UIKit controls have the cut, copy, paste behaviors. We can also mimic this behavior in SQLite data, images and other controls which earlier did not have this property.

Copy contents from an App

//To copy an image to pasteboard

-(IBAction)copyImageToPasteBoard{

UIPasteboard *pasteBoard = [UIPasteboard pasteboardWithName:@”CopyFrom” create:YES];

pasteBoard.persistent = YES;

NSData *data = UIImagePNGRepresentation([UIImage imageNamed:kYourImageName]);

[pasteBoard setData:data forPasteboardType:kYourPasteboardType];

//Paste board type is usually a UTI

}

//To copy a string to pasteboard

-(IBAction)copyStringToPasteBoard{

UIPasteboard *pasteBoard = [UIPasteboard pasteboardWithName:@”CopyFrom” create:YES];

pasteBoard.persistent = YES;

[pasteBoard setString:yourTextField.text];

}

Paste contents in to another App

To get the data shared through the pasteboard in another app use the following code:

-(IBAction)pasteImageToPasteBoard{

UIPasteboard * pasteBoard = [UIPasteboard pasteboardWithName:@”CopyFrom” create:YES];

NSData *data = [pasteBoard dataForPasteboardType:kYourPasteboardType];

imageView.image = [UIImage imageWithData:data];

}

-(IBAction)pasteStringToPasteBoard{

UIPasteboard * pasteBoard = [UIPasteboard pasteboardWithName:@”CopyFrom” create:YES];

yourTextField.text = [pasteBoard string];

}

This way you can share any kind of data across the Apps using pasteboard.

Advertisements