我在 SwiftUI 中使用 .fileImporter 修饰符在我的应用程序中导入 pdf 文件。我有几个问题。首先,数据加载速度很慢,并且通常无法加载并给出消息“内部错误无法与帮助程序应用程序通信”。
但主要问题是,如果向下滑动关闭模态视图,则无法再次呈现。大概是因为绑定 $showFileImporter 未重置。如果按下取消按钮来关闭它就可以正常工作。我不知道是否有办法强制它全屏来解决这个问题。
这是我的代码:
.fileImporter(isPresented: $showFileImporter, allowedContentTypes: [.pdf]) { result in
switch result {
case .success(let url):
url.startAccessingSecurityScopedResource()
if let pDFDocument = PDFDocument(url: url) {
if let data = pDFDocument.dataRepresentation() {
// handle data
}
}
case .failure(let error):
print(error)
}
}
Run Code Online (Sandbox Code Playgroud) 尝试使用以下代码块读取文档数据
\n.fileImporter(isPresented: $showDocPicker, allowedContentTypes: [SupportDocTypes], onCompletion: { result in\n do {\n let fileURL = try result.get()\n let docData = try Data(contentsOf:fileURL) // <-error on this line\n } catch {\n print ("error reading")\n print (error.localizedDescription)\n }\n})\nRun Code Online (Sandbox Code Playgroud)\n它给出了以下错误
\n\n\n错误域=NSCocoaErrorDomain代码=257“文件\xe2\x80\x9ca.txt\xe2\x80\x9d无法\xe2\x80\x99打开,因为您\xe2\x80\x99没有查看它的权限。” \nUserInfo={NSFilePath=/private/var/mobile/Containers/Data/Application/CCB51769-028A-4C15-A2C2-3A263791BA08/Documents/a.txt,\nNSUnderlyingError=0x283e6a4f0 {错误域=NSPOSIXErrorDomain代码=1\n"不允许操作”}}
\n
此错误仅发生在物理设备上。\nXcode 13.2.1\n在 iOS 15.2 上测试 | 14.8
\n