我有以下代码来读取传递的URL.我正在使用Pocket应用程序对此进行测试,虽然hasItemConformingToTypeIdentifier为kUTTypeURL返回YES,但尝试加载它会返回错误而不是说明
“Unexpected value class.”
.如果我尝试将其加载为id< NSSecureCoding> item和debug,我发现传入的对象确实只是页面的标题而不是URL.我如何阅读URL?
NSURL *pageURL = nil; for (NSExtensionItem *item in self.extensionContext.inputItems) { for (NSItemProvider *itemProvider in item.attachments) { if ([itemProvider hasItemConformingToTypeIdentifier: (NSString*) kUTTypeURL]) { [itemProvider loadItemForTypeIdentifier:(NSString*) kUTTypeURL options:nil completionHandler:^(id <NSSecureCoding> urlItem,NSError *error) { if ([((NSObject*)urlItem) isKindOfClass: [NSURL class]]) { pageURL = [((NSURL*)urlItem) absoluteString]; } }]; } } }
解决方法
如果您阅读以下文档:
loadItemForTypeIdentifier(_:options:completionHandler:)
你会看到:
The type information for the first parameter of your completionHandler
block should be set to the class of the expected type. For example,
when requesting text data,you might set the type of the first
parameter to NSString or NSAttributedString. An item provider can
perform simple type conversions of the data to the class you specify,
such as from NSURL to NSData or NSFileWrapper,or from NSData to
UIImage (in iOS) or NSImage (in OS X). If the data could not be
retrieved or coerced to the specified class,an error is passed to the
completion block’s.
也许你可以通过胁迫不同的类型进行实验?