iOS共享扩展程序未获取图像 [英] iOS share extension not getting the image

查看:177
本文介绍了iOS共享扩展程序未获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,一旦有人将我指向正确的方向,这是微不足道的,因此,我为提出一个愚蠢的问题道歉.但是,我一直在寻找无法弄清楚自己在做错什么的日子.

I am sure this is trivial once someone kindly point me in the right direction so my apology for asking a silly question. However I have been searching for days I can't figure out what I am doing wrong.

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

Scenario: create a simple share extension that receives an image file

问题:当我访问附件时,永远不会调用该处理程序,尽管我可以在itemProvider中看到"public.jpg",但看不到数据在哪里?

Problem: when I access the attachements, the handler is never called albeit I can see the "public.jpg" in the itemProvider but I can't see where the data would be?

我做了什么:

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

推荐答案

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

You haven't posted your complete code but I suspect that you are calling completeRequestReturningItems:completionHandler: at the wrong location:

错误:

- (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已被释放.

The problem is that calling completeRequestReturningItems:completionHandler: immediately dismisses the ShareViewController and deallocates it. So the NSItemProvider that contains the image is also destroyed before it can access the image (because it loads its items asynchronously). In other words: the completion handler in which you send the image to your server is never called, because the whole shareViewController has already been deallocated.

要解决该问题,您必须在发送图像后将调用移至completeRequestReturningItems:completionHandler:到完成处理程序的末尾.

To fix that problem you have to move the call to completeRequestReturningItems:completionHandler: to the end of the completion handler AFTER you send the image.

正确:

- (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];
                              }];
    }
}

这篇关于iOS共享扩展程序未获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆