解码 UIPasteboard 上的“Apple Web Archive 粘贴板类型”项目

Run*_*oop 3 iphone cocoa-touch ipad ios

我正在复制一个网页,想看看复制到 UIPasteBoard 的字典是如何组成的。我目前在通用粘贴板上记录该项目,如下所示:

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

for (NSDictionary *dict in pasteboard.items) {

    NSLog(@"Dict: %@", dict);
}
Run Code Online (Sandbox Code Playgroud)

输出是:

Dict: {
    "Apple Web Archive pasteboard type" = <62706c69 73743030 d2010203 0d5f100f 57656253 75627265 736f7572 6365735f 100f5765 624d6169 6e526573 6f757263 65a104d4 05060708 090a0b0c 5e576562 5265736f 75726365 55524c5f 100f5765.............
Run Code Online (Sandbox Code Playgroud)

我尝试获取“Ap​​ple Web Archive 粘贴板类型”键的字符串,如下所示,但没有成功:

NSString *string = [[NSString alloc] initWithData:[dict objectForKey:@""Apple Web Archive pasteboard type""] encoding:NSUTF8StringEncoding];

NSLog(@"item %@", string);
Run Code Online (Sandbox Code Playgroud)

请问如何解码该密钥的数据?

小智 5

Apple Web Archive 粘贴板类型为 Plist,用于发现密钥 - 使用 Plist 编辑器打开。下面还有一段简短的代码(从粘贴的网页获取图像信息)

if ([[[UIPasteboard generalPasteboard] pasteboardTypes] containsObject:WEB_ARCHIVE]) {
    NSData* archiveData = [[UIPasteboard generalPasteboard] valueForPasteboardType:WEB_ARCHIVE];
    if (archiveData)
    {
        NSError* error = nil;
        id webArchive = [NSPropertyListSerialization propertyListWithData:archiveData options:NSPropertyListImmutable format:NULL error:&error];
        if (error) {
            return;
        }
        NSArray *subItems = [NSArray arrayWithArray:[webArchive objectForKey:@"WebSubresources"]];
        NSPredicate *iPredicate = [NSPredicate predicateWithFormat:@"WebResourceMIMEType like 'image*'"];
        NSArray *imagesArray = [subItems filteredArrayUsingPredicate:iPredicate];
        for (int i=0; i<[imagesArray count]; i++) {
            NSDictionary *sItem = [NSDictionary dictionaryWithDictionary:[imagesArray objectAtIndex:i]];
            UIImage *sImage = [UIImage imageWithData:[sItem valueForKey:@"WebResourceData"]];
            // handle images
        }
    }

}
Run Code Online (Sandbox Code Playgroud)