【问题标题】:iOS Share Extension issue with ItemProvider can't read from Photo library无法从照片库中读取 ItemProvider 的 iOS 共享扩展问题
【发布时间】:2018-02-18 13:16:27
【问题描述】:

我使用解决方案表单iOS Share Extension issue when sharing images from Photo library 从照片应用程序中获取图像。这在模拟器中效果很好,但在设备上我收到一个错误,我无法访问 itemProvider 提供的 NSURL:

2018-02-18 12:54:09.448148+0100 MyApp[6281:1653186] [default] [ERROR] 无法确定 URL /var/mobile/Media/PhotoData/OutgoingTemp/554581B2-950C-4CFD-AE67 -A2342EDEA04D/IMG_2784.JPG (s) 由文件提供者管理

由语句引起:

[itemProvider loadItemForTypeIdentifier:itemProvider.registeredTypeIdentifiers.firstObject options:nil completionHandler:^(id<NSSecureCoding> item, NSError *error) {
}

在 PHAsset 中搜索项目名称并不是一个好的解决方案,因为用户必须再次访问照片库。

【问题讨论】:

  • 我也面临同样的问题。你找到解决方案了吗?
  • 看起来没有解决办法。目前,我的代码尝试从 URL 加载 NSData,如果失败,我从 PHAsset 加载它(构建所有 PHAsset 的数组并搜索最后一个路径组件作为文件名。)非常糟糕的代码,但在我的测试中它适用于我的所有应用程序允许图像共享。

标签: ios share-extension


【解决方案1】:

在 didSelectPost 中,在处理完 inputItems 数组中的所有项目之前,不得关闭 ShareExtension ViewController。 ViewController 使用 [super didSelectPost] 或通过调用扩展上下文的完成方法来解除。

这是我的代码解决方案:

- (void)didSelectPost {

__block NSInteger itemCount = ((NSExtensionItem*)self.extensionContext.inputItems[0]).attachments.count;
__block NSInteger processedCount = 0;
for (NSItemProvider* itemProvider in ((NSExtensionItem*)self.extensionContext.inputItems[0]).attachments ) {

    if([itemProvider hasItemConformingToTypeIdentifier:@"public.jpeg"]) {
        NSLog(@"itemprovider = %@", itemProvider);

        [itemProvider loadItemForTypeIdentifier:@"public.jpeg" options:nil completionHandler: ^(id<NSSecureCoding> item, NSError *error) {

            NSData *imgData;
            if([(NSObject*)item isKindOfClass:[NSURL class]]) {
                imgData = [NSData dataWithContentsOfURL:(NSURL*)item];
            }
            if([(NSObject*)item isKindOfClass:[UIImage class]]) {
                imgData = UIImagePNGRepresentation((UIImage*)item);
            }
            NSDictionary *dict = @{
                                   @"imgData" : imgData,
                                   @"name" : self.contentText
                                   };
            NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.share.extension1"];
            [defaults setObject:dict forKey:@"img"];
            [defaults synchronize];
            processedCount += 1;
            if (processedCount == itemCount)
                [super didSelectPost];
        }];
    }
}

【讨论】:

  • 我的解决方案看起来很相似,另外我还有一个 if: if( [(NSObject*)item isKindOfClass:[NSURL class]] ) 并从 PHPhotoLibrary 加载图像。
  • 我为此苦苦挣扎了好几个小时...谢谢!
【解决方案2】:

loadItemForTypeIdentifier 或 Swift 中的 loadItem 方法是异步,因此必须将关闭 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()

async 方法 loadItem 之后在 completionHandler 之外,您会收到各种权限错误,而且这些错误可能是随机的,有时会发生有时不会,这是因为有时您对 loadItemasync 调用有机会在 UI 关闭之前终止,有时则不会。

把这个留在这里,希望它对某人有所帮助。这个问题花了我几个小时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多