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!
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:
-(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]; }
-(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]; }
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.
UIPasteBoard
pasteboardWithName
string
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!
CodeProject
Please share this if you like it!
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)