Storing NSArray in UIPasteboard

烈酒焚心 提交于 2019-12-01 05:47:00

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

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