Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / iPhone

How to Use UIPasteBoard to Implement Custom Copy and Paste In Your App

2.25/5 (5 votes)
2 Nov 2009CPOL1 min read 29.5K   196  
Here is how to use UIPasteBoard to implement custom copy and paste in your app

braintrade

Finally, we can share data between our apps using custom copy and paste in the latest version of iPhone OS. Here is how you do it!

Copy and Paste in UIKit

As you know, many of the controls in UIKit now come pre-loaded with the ability to copy and paste text. You can also use this new ability in your own apps to copy and paste other things including: images, SQLite databases, text or any file. This is a great way to share data between your apps if you want to provide users with a suite of apps with integrated functionality.

…also, it is very easy. Check out the video below:

Implementing UIPasteBoard

CopyFrom Source Code

C++
-(IBAction)copyImageToPasteBoard{
	UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyFrom" 
             create:YES];
	appPasteBoard.persistent = YES;

	NSData *data = UIImagePNGRepresentation([UIImage imageNamed:@"Old-Time-Photo.jpg"]);
	[appPasteBoard setData:data forPasteboardType:@"com.appshop.copyfrom.imagedata"];
}

-(IBAction)copyStringToPasteBoard{
	UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyFrom" 
             create:YES];
	appPasteBoard.persistent = YES;
	[appPasteBoard setString:textView.text];
}	

PasteTo Source Code

C++
-(IBAction)pasteImageToPasteBoard{
	UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyFrom"
             create:YES];
	NSData *data = [appPasteBoard dataForPasteboardType:@"com.appshop.copyfrom.imagedata"];
	imageView.image = [UIImage imageWithData:data];
}

-(IBAction)pasteStringToPasteBoard{
	UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyFrom" 
             create:YES];
	textView.text = [appPasteBoard string];
}	

Recap

Using UIPasteBoard in iPhone programming is amazingly simple and opens up some possibilities that we did not have a year ago. To use UIPasteBoard, you simply create a instance using pasteboardWithName, put stuff into the paste board and then set the persistent property equal to YES. Then any app can get a reference to your paste board and use the data contained within. You can use it for simple strings and even data that you can put into NSData like SQLite databases.

Do You Have Any Tips For Using UIPasteBoard?

Please let us know by commenting below!

P.S.: If you are a member of my mailing list, stay tuned for details on how to get the complete source code for these projects.  If you are not, sign up today before you miss out!

Please share this if you like it!

Digg del.icio.us Facebook Mixx Google Bookmarks email FriendFeed LinkedIn MySpace Ping.fm StumbleUpon Suggest to Techmeme via Twitter Technorati Tumblr Yahoo! Bookmarks

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)