Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy and Paste on iPhone with multiple data representations

I encountered some issues when trying to put more than one data representation onto the pasteboard on iPhone 3.0.

What I'm trying to do is put a data representation and a string representation onto the pasteboard. The data is my own data type and I use it for copy and paste in my application. The string representation is a way to copy and paste the content of my application as an outline into an other application (for example Mail.app).

    // payload
NSString *pasteboardString = [selectedNode stringRepresentation];
NSDictionary *pasteboardDictionary = [selectedNode nodeAndSubnodesProperties];

// set payload
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = pasteboardString;
[pasteboard setValue:pasteboardDictionary forPasteboardType:MNTNodesPasteboardType];

The above code doesn't work because the string property and setValue:forPasteboardType: methode replace the first representation on the pasteboard. I tried addItems: but it didn't work for me.

Thank you for any help!

like image 274
Markus Müller-Simhofer Avatar asked Jun 23 '09 19:06

Markus Müller-Simhofer


People also ask

How do you copy multiple things on iPhone?

In both iOS and Android, tap and hold on the text you want to copy, then drag the selector lines around all the text you want to copy, and tap Copy. Then tap and hold anywhere where you want to paste the text, and tap Paste.

How do you easily copy and paste on iPhone?

Cut: Tap Cut or pinch closed with three fingers two times. Copy: Tap Copy or pinch closed with three fingers. Paste: Tap Paste or pinch open with three fingers.


1 Answers

To answer my own question:

You have to used the items property to put multiple representations onto the pasteboard. To do so you create a dictionary with each representation as the value and the representation type as the key. Add this dictionary to an array, where each item in the array represents an item (UIPasteboard supports adding multiple items to the pasteboard as well as adding mutliple representation to each item).

Example code for one single item with two representations:

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSMutableDictionary *item = [NSMutableDictionary dictionaryWithCapacity:2];
[item setValue:[NSKeyedArchiver archivedDataWithRootObject:pasteboardDictionary] forKey:MNTNodesPasteboardType];
[item setValue:pasteboardString forKey:(NSString *)kUTTypeUTF8PlainText];
pasteboard.items = [NSArray arrayWithObject:item];

Don't forget to link with the MobileCoreServices framework to resolve the UTI constant.

like image 107
Markus Müller-Simhofer Avatar answered Oct 24 '22 16:10

Markus Müller-Simhofer