loadItemForTypeIdentifier
Swift 中的orloadItem
方法是异步的,因此关闭 UI 必须作为其completionHandler
.
例如我有:
override func didSelectPost() {
// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
if let item = self.extensionContext?.inputItems[0] as? NSExtensionItem, let attachments = item.attachments {
for provider in attachments {
if provider.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {
provider.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil, completionHandler: {item, error in
// do all you need to do here, i.e.
if let tmpURL = item as? URL {
// etc. etc.
}
// and, at the end, inside the completionHandler, call
// Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
})
}
}
}
}
我通过以下方式关闭 UI:
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
或通过:
super.didSelectPost()
在completionHandler
after the async方法之外,loadItem
您将获得各种权限错误,此外,这些错误可能是随机的,有时会发生,有时不会,这是因为有时您的异步调用loadItem
有机会在 UI 被关闭之前终止有时它不会。
把这个留在这里,希望它可以帮助某人。这个问题花了我几个小时。