ios – Share extension loadItemForTypeIdentifier返回NSURL的错误

前端之家收集整理的这篇文章主要介绍了ios – Share extension loadItemForTypeIdentifier返回NSURL的错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下代码来读取传递的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.

也许你可以通过胁迫不同的类型进行实验?

原文链接:https://www.f2er.com/iOS/333411.html

猜你在找的iOS相关文章