OGeek|极客世界-中国程序员成长平台

标题: iOS 共享扩展未获取图像 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:41
标题: iOS 共享扩展未获取图像

我敢肯定,一旦有人好心地为我指出正确的方向,这就变得微不足道了,所以我很抱歉问了一个愚蠢的问题。但是我一直在寻找几天我无法弄清楚我做错了什么。

场景:创建一个接收图像文件的简单共享扩展

问题:当我访问附件时,处理程序从未被调用,尽管我可以在 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 hasItemConformingToTypeIdentifierNSString *)kUTTypeImage]) {
I can hit this breakpoint --> [itemProvider loadItemForTypeIdentifierNSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
but not this one --> photo = image;
            }];
            break;
        }
    }
}
.... and so on and so forth



Best Answer-推荐答案


您还没有发布完整的代码,但我怀疑您在错误的位置调用了 completeRequestReturningItems:completionHandler::

错误:

- (void)didSelectPost {
        NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
        NSItemProvider *itemProvider = item.attachments.firstObject;
        if ([itemProvider hasItemConformingToTypeIdentifierNSString *)kUTTypeImage]) {
            [itemProvider loadItemForTypeIdentifierNSString *)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 hasItemConformingToTypeIdentifierNSString *)kUTTypeImage]) {
        [itemProvider loadItemForTypeIdentifierNSString *)kUTTypeImage
                                        options:nil
                              completionHandler:^(NSURL *url, NSError *error) {
                                  // send the image
                                  [self.extensionContext completeRequestReturningItems[]         
                                                                     completionHandler:nil];
                              }];
    }
}

关于iOS 共享扩展未获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32525448/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4