Storing NSArray in UIPasteboard

断了今生、忘了曾经 提交于 2019-12-19 07:35:22

问题


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


回答1:


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




回答2:


As of iOS 8.3, UIPasteboard still has this bug. I wrote an extension for UIPasteboard to handle this:

extension UIPasteboard {
    func arrayForPasteboardType(pasteboardType: String) -> NSArray? {
        switch valueForPasteboardType(pasteboardType) {
        case let array as NSArray:
            return array
        case let data as NSData:
            if let array = NSPropertyListSerialization.propertyListWithData(data, options: 0, format: nil, error: nil) as? NSArray {
                return array
            }
        default:
            break
        }

        return nil
    }
}


来源:https://stackoverflow.com/questions/9164278/storing-nsarray-in-uipasteboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!