gpt4 book ai didi

ios - 共享扩展 loadItemForTypeIdentifier 为 NSURL 返回错误

转载 作者:可可西里 更新时间:2023-11-01 03:19:28 27 4
gpt4 key购买 nike

我有以下代码来读取传递的 URL。我正在用 Pocket 测试这个应用程序虽然 hasItemConformingToTypeIdentifier正在为 kUTTypeURL 返回 YES ,尝试加载它会返回一个错误,而不是声明

"Unexpected value class."

.如果我尝试将其加载为 id<NSSecureCoding> item和debug,发现传入的object确实只是页面的title,并不是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.

也许您可以通过强制转换为不同的类型来进行试验?

关于ios - 共享扩展 loadItemForTypeIdentifier 为 NSURL 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28327131/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com