NSItemProvider loadItem方法返回_NSItemProviderSandboxedResource而不是URL [英] NSItemProvider loadItem method returns _NSItemProviderSandboxedResource instead of URL

查看:602
本文介绍了NSItemProvider loadItem方法返回_NSItemProviderSandboxedResource而不是URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我开发的其中一个应用程序中,我使用共享扩展名来导入电子钱包通行证(PKPass).

In one of the apps I develop, I use a share extension to import Wallet passes (PKPass).

在iOS 13(包括最新的Beta 8)上,当从iOS Mail应用程序中调用共享扩展名时,该扩展名未接收到预期格式(URL)的数据.

On iOS 13 (including the latest beta 8), when calling the share extension from within the iOS Mail app, the extension is not receiving the data in the expected format (URL).

这是共享扩展的ShareViewController中的相关代码段:

This is the relevant snippet from the share extension’s ShareViewController:

if let inputItems = self.extensionContext?.inputItems,
   let inputItem = inputItems.first as? NSExtensionItem,
   let attachments = inputItem.attachments,
   let attachment = attachments.first,
   attachment.hasItemConformingToTypeIdentifier("com.apple.pkpass" as String){
    attachment.loadItem(forTypeIdentifier: "com.apple.pkpass" as String, options: nil) { data, error in
        print ("data: \(String(describing: data))")
}

在iOS 12(最新版本)上,这对于iOS Mail应用程序中的附件(例如,文件应用程序中的文件)也可以正常工作;数据包含一个可选的URL. 上面的打印语句在控制台中显示如下:

On iOS 12 (latest version) this works fine also for an attachment in the iOS Mail app (in addition e.g. to a file in the Files app); data holds an optional URL. The print statement above shows as follows in the console:

data: Optional(file:///var/mobile/Library/Mail/8EF174CF-68B9-414E-A166-D04C9DBE020E/INBOX.imapmbox/Attachments/13846/2/Attachment-1.pkpass)

在iOS 13(测试版8)的iOS Mail应用程序中,数据包含一个可选的_NSItemProviderSandboxedResource. 上面的打印语句在控制台中显示如下:

On iOS 13 (beta 8), in the iOS Mail app, data holds an optional _NSItemProviderSandboxedResource. The print statement above shows as follows in the console:

data: Optional(<_NSItemProviderSandboxedResource: 0x2839aa9e0>)

这似乎仅影响邮件应用程序.在文件"应用中,数据保留了-如预期的那样-一个URL.

This seems to affect only the Mail app. In the Files app data holds - as expected - an URL.

这是错误(实际上我已经在beta 4上使用反馈助手报告了此错误)还是iOS 13引入了一些新的安全功能?在这种情况下,如何访问附件的url/数据?

Is this a bug (actually I have already reported this using Feedback Assistant back on beta 4) or some new security feature introduced by iOS 13? How can I access the attachment's url/data in this case?

推荐答案

刚刚找到了解决此问题的方法.

Just found a solution for this issue.

在致电iOS 13之前

Prior to iOS 13 when calling

attachment.loadItem(forTypeIdentifier: "com.apple.pkpass" as String, options: nil) data, error in

数据也可以按如下方式下放到URL

data could be downcasted also to an URL as follows

if let pkPassURL = data as? URL

如上面的问题所述,在iOS 13中这不再可能.

As described in the question above, in iOS 13 this is not possible any longer.

相反,现在需要使用"public.file-url"作为类型标识符来调用loadItem:

Instead, it is now required to call loadItem with "public.file-url" as type identifier:

attachment.loadItem(forTypeIdentifier: "public.file-url" as String, options: nil) { (data, error) in

在PKPass项目的特定情况下,我注意到共享它们时

In the specific case of PKPass items, I have noticed that when sharing them

  • 在电子钱包应用中,附件仅符合"com.apple.pkpass"
  • 例如邮件应用程序的附件将同时符合"com.apple.pkpass"和"public.file-url"

因此以下代码可用于处理两种情况:

So following code can be used to deal with both cases:

if let inputItems = self.extensionContext?.inputItems,
   let inputItem = inputItems.first as? NSExtensionItem,
   let attachments = inputItem.attachments,
   let attachment = attachments.first,
   attachment.hasItemConformingToTypeIdentifier("com.apple.pkpass" as String) {
    if attachment.hasItemConformingToTypeIdentifier("public.file-url" as String) {
        // extension is being called e.g. from Mail app
        attachment.loadItem(forTypeIdentifier: "public.file-url" as String, options: nil) { (data, error) in 
            if let sourcePKPassURL = data as? URL {
                //handle url here
            }
        }
    } else {
        // extension is being called from Wallet app
        attachment.loadItem(forTypeIdentifier: "com.apple.pkpass" as String, options: nil) { (data, error) in
            if let pkPassData = data as? Data,
               let pkPass = try? PKPass(data: pkPassData) {
               // handle pkPass here
            }
        }
    }
}

这同时适用于iOS 12和iOS 13.

This works for both iOS 12 and iOS 13.

这篇关于NSItemProvider loadItem方法返回_NSItemProviderSandboxedResource而不是URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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