Click here to Skip to main content
15,881,173 members
Articles / Mobile Apps / iPhone

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

Rate me:
Please Sign up or sign in to vote.
2.25/5 (5 votes)
2 Nov 2009CPOL1 min read 28.9K   196   10   3
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
berna yukioj19-Jul-10 10:15
berna yukioj19-Jul-10 10:15 
GeneralMy vote of 1 Pin
Alexandre GRANVAUD16-Nov-09 20:38
Alexandre GRANVAUD16-Nov-09 20:38 
GeneralMy vote of 1 Pin
DanWalker4-Nov-09 7:01
DanWalker4-Nov-09 7:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.