Skip to content
Tags

, ,

UIPasteBoard to implement copy, paste in your app

June 21, 2010

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

From → iPhone

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: