Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing NSArray in UIPasteboard

I have several text files which I want to transfer between 2 Apps. (ie. free and paid versions of the same App).

I'm using UIPasteboard to do this. The contents of the files are held in memory as NSArrays, and so I want to copy these NSArrays to the pasteboard (lite version), and read them from the pasteboard (full version).

For some reason the data cannot be read back from the pasteboard. The data is being returned as a NSData object, rather than NSArray, which I think means that it is not in the required format for the pasteboard type I am using, which is "public.utf8-plain-text".

When I read/write NSStrings with this pasteboard type, it works fine.

I searched through Apple docs, etc, to see if there is a different type I should be using for NSArrays, (or other property list objects), but drew a blank.

Writing to the pasteboard: (In the following pDataOutput is an array of strings, file contents) :

NSMutableArray *lArrayCopy = [gGlobalData.cPasteBoard.items mutableCopy];
[lArrayCopy replaceObjectAtIndex:pDataFileIdx
                  withObject:[NSDictionary dictionaryWithObject:pDataOutput
                                                         forKey:@"public.utf8-plain-text"]];
gGlobalData.cPasteBoard.items = lArrayCopy;
[lArrayCopy release];

Reading from the pasteboard:

NSArray *lPBItems = [pPasteBoard valuesForPasteboardType:@"public.utf8-plain-text"
                                               inItemSet:nil];
NSLog(@"PB Items = NSArray of count %d", lPBItems.count);

The above returns:

PB Items = NSArray of count 0

As mentioned above, it returns the data correctly as NSStrings if written as NSStrings.

Any help would be very much appreciated. Thanks Stephen C

like image 774
stephencampbell Avatar asked Feb 06 '12 17:02

stephencampbell


1 Answers

I ran into the same issue and I think the valueForPasteboardType family of methods are broken and always return NSData. Here is my solution:

NSArray * lArrayFromPasteBoard = [pPasteBoard valueForPasteboardType:@"com.my.custom.type"];
if ([lArrayFromPasteBoard isKindOf:[NSData class]])
{
    lArrayFromPasteBoard = [[NSPropertyListSerialization propertyListWithData:(NSData*)lArrayFromPasteBoard options:0 format:0 error:0];
}

hopefully this will make it so the code in the if won't get called anymore once apple fixes their bug

like image 57
terrinecold Avatar answered Jan 01 '23 13:01

terrinecold