UIDocumentPickerViewController自定义APK的UTI在iOS16.1无法选取文件的兼容问题
问题
我用以下代码实现UIDocumentPickerViewController:
if #available(iOS 14.0, *) { let apkType = UTType("com.coocaa.apk") let docPickerVC = UIDocumentPickerViewController(forOpeningContentTypes: [apkType!]) docPickerVC.delegate = self present(docPickerVC, animated: true) } else { let types = ["com.coocaa.apk"] let docPickerVC = UIDocumentPickerViewController(documentTypes: types, in: .open) present(docPickerVC, animated: true) }
目前该代码在iOS14和iOS16.5系统中可正常选取.apk文件,但在iOS16.1系统中无法选取。我已在info.plist中添加如下配置:
<key>UTExportedTypeDeclarations</key> <key>UTExportedTypeDeclarations</key> <array> <dict> <key>UTTypeConformsTo</key> <array> <string>public.data</string> <string>public.archive</string> </array> <key>UTTypeDescription</key> <string>apk statements</string> <key>UTTypeIdentifier</key> <string>com.coocaa.apk</string> <key>UTTypeTagSpecification</key> <dict> <key>public.filename-extension</key> <array> <string>apk</string> </array> </dict> </dict> </array>
请问如何调整才能让UIDocumentPickerViewController在所有iOS系统中都允许选取.apk文件?
解决方案
1. 修复info.plist的重复键与补全UTI配置
你的UTExportedTypeDeclarations键重复,会导致部分iOS版本解析UTI异常。删除重复键,并补充APK的标准MIME类型标签,让系统更精准识别文件:
<key>UTExportedTypeDeclarations</key> <array> <dict> <key>UTTypeConformsTo</key> <array> <string>public.data</string> <string>public.archive</string> </array> <key>UTTypeDescription</key> <string>APK Package File</string> <key>UTTypeIdentifier</key> <string>com.coocaa.apk</string> <key>UTTypeTagSpecification</key> <dict> <key>public.filename-extension</key> <array> <string>apk</string> </array> <key>public.mime-type</key> <string>application/vnd.android.package-archive</string> </dict> </dict> </array>
2. 调整代码兼容多版本iOS
避免强制解包UTType,同时添加通用public.data作为兜底类型,弥补自定义UTI在特定版本的识别漏洞:
if #available(iOS 14.0, *) { let apkUTI = UTType("com.coocaa.apk") ?? UTType.data let docPickerVC = UIDocumentPickerViewController(forOpeningContentTypes: [apkUTI, .data]) docPickerVC.delegate = self present(docPickerVC, animated: true) } else { let types = ["com.coocaa.apk", "public.data"] let docPickerVC = UIDocumentPickerViewController(documentTypes: types, in: .open) present(docPickerVC, animated: true) }
3. 验证UTI配置有效性
建议通过Xcode的Info标签页可视化编辑UTI,避免手动编写XML时出现格式错误,确保系统能正确读取自定义UTI规则。
内容的提问来源于stack exchange,提问作者Kenny
相关产品推荐
相关产品推荐

