Sunday, November 30, 2014

Pasteboard in iOS interprocess Communication


On iOS, the class is UIPasteBoard, and on Mac, it is NSPasteBoard. These both are pretty much the same, but on iOS, the APIs are modern and cleaner. 
Programmatically writing into pasteboard is nearly as invoking Edit > Copy in GUI application. 

NSImage *image; 
NSPasteBoard *pasteboard = [NSPasteBoard generalPasteboard];
[pasteboard clearContents];
[pasteboard writeObjects:@[image]];

The receive of contents in the pasteboard is bit more involved and it is like below 

NSPasteBoard *pasteboard = [NSPasteBoard generalPasteboard];
if([pasteboard canReadObjectForClasses:@[[NSImage image] options:nil])
{
NSArray *contents = [pasteboard readObjectsForClasses:@[[NSImage class]] options:nil];
NSImage *image = [contents firstObject];
}


What makes Pasteboard compelling as a mechanism for transferring data is the notion of simultaneously providing multiple representations of content copied into pasteboard. For e.g. a selection of text may be copied as a Rich text or a normal text and the receiver can be accordingly configured to take it. For e.g. a rich text editor will be able to take the formatted text. 

References:
http://nshipster.com/inter-process-communication/

No comments:

Post a Comment