Storing NSArray in UIPasteboard

后端 未结 2 1186
小鲜肉
小鲜肉 2021-01-13 08:45

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

相关标签:
2条回答
  • 2021-01-13 09:24

    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

    0 讨论(0)
  • 2021-01-13 09:32

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题