ItemProvider的iOS Share Extension问题无法从Photo库

时间:2018-02-18 13:16:27

标签: ios share-extension

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

2018-02-18 12:54:09.448148 + 0100 MyApp [6281:1653186] [默认] [错误]无法确定是否为URL / var / mobile / Media / PhotoData / OutgoingTemp / 554581B2-950C-4CFD-AE67 -A2342EDEA04D / IMG_2784.JPG由文件提供商管理

由法令引起:

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

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

2 个答案:

答案 0 :(得分:1)

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

这是我在代码中的解决方案:

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

答案 1 :(得分:0)

module.exports = async (req, res) => { ... some code .... } 或 Swift 中的 request = {'earningsCalendar': [{'date': '2021-04-27', 'epsActual': None, 'epsEstimate': None, 'hour': 'bmo', 'quarter': 1, 'revenueActual': None, 'revenueEstimate': None, 'symbol': 'ALHC', 'year': 2021}, {'date': '2021-04-27', 'epsActual': None, 'epsEstimate': None, 'hour': 'amc', 'quarter': 1, 'revenueActual': None, 'revenueEstimate': None, 'symbol': 'FRST', 'year': 2021}]} 方法是异步,因此关闭 UI 必须作为其 loadItemForTypeIdentifier 中的最后一件事被调用。

例如我有:

loadItem

我通过以下方式关闭用户界面:

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)
                })
            }
        }
    }
}

async 方法 self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil) 之后的 super.didSelectPost() 之外,您将收到各种权限错误,而且这些错误可能是随机的,有时会发生有时不会' t,这是因为有时您对 completionHandlerasync 调用有机会在 UI 关闭之前终止,而有时则没有。

就留在这里吧,希望对大家有所帮助。这个问题花了我几个小时。