我确信,一旦有人善意地指出我正确的方向,这是微不足道的,所以我向你提出一个愚蠢的问题的道歉。但是我一直在寻找几天我无法弄清楚我做错了什么。
场景:创建一个接收图像文件的简单共享扩展程序
问题:当我访问附件时,永远不会调用处理程序,虽然我可以在itemProvider中看到“public.jpg”,但我看不到数据的位置?
我做了什么:
1) defined NSExtensionActivationSupportsImageWithMaxCount = 1 as my only activation rule
2) added CoreMedia framework to the extension
3) added the same group to both app and app extension
4) made sure both have the group (1) in the entitlement
5) made sure both are using a certificate/app id with that group enabled
6) clean and rebuild several times to no avail.
代码:
- (void)didSelectPost {
/
for (NSExtensionItem *item in self.extensionContext.inputItems) {
for (NSItemProvider *itemProvider in item.attachments) {
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
I can hit this breakpoint --> [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
but not this one --> photo = image;
}];
break;
}
}
}
.... and so on and so forth
答案 0 :(得分:4)
您尚未发布完整的代码,但我怀疑您在错误的位置呼叫completeRequestReturningItems:completionHandler:
:
<强> WRONG 强>:
- (void)didSelectPost {
NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
NSItemProvider *itemProvider = item.attachments.firstObject;
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage
options:nil
completionHandler:^(NSURL *url, NSError *error) {
// send the image
}];
}
// ↓ this is the wrong location ↓
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}
问题是调用completeRequestReturningItems:completionHandler:
会立即解除ShareViewController并取消分配它。因此,包含图像的NSItemProvider在它可以访问图像之前也会被销毁(因为它异步加载它的项目)。换句话说:永远不会调用将映像发送到服务器的完成处理程序,因为整个shareViewController已经被释放。
要解决此问题,您必须在发送图像后将调用移至completeRequestReturningItems:completionHandler:
到完成处理程序的末尾。
<强>正确:强>
- (void)didSelectPost {
NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
NSItemProvider *itemProvider = item.attachments.firstObject;
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage
options:nil
completionHandler:^(NSURL *url, NSError *error) {
// send the image
[self.extensionContext completeRequestReturningItems:@[]
completionHandler:nil];
}];
}
}