Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在真实设备上拾取PDF时,iOS文档选择器崩溃_Ios_Swift_Xcode_Swiftui - Fatal编程技术网

在真实设备上拾取PDF时,iOS文档选择器崩溃

在真实设备上拾取PDF时,iOS文档选择器崩溃,ios,swift,xcode,swiftui,Ios,Swift,Xcode,Swiftui,我尝试为我的iOS应用程序创建文档选择器 以下是我的代码(我在SwiftUI视图中包装了UIDocumentPickerViewController,并使用UIViewControllerRepresentable): 导入快捷界面 进口流动储备 结构DocumentPickerViewController:UIViewControllerRepresentable{ 变量回调:(数据)->() func makeCoordinator()->Coordinator{ 退货协调员(documen

我尝试为我的iOS应用程序创建文档选择器

以下是我的代码(我在SwiftUI视图中包装了UIDocumentPickerViewController,并使用UIViewControllerRepresentable):

导入快捷界面
进口流动储备
结构DocumentPickerViewController:UIViewControllerRepresentable{
变量回调:(数据)->()
func makeCoordinator()->Coordinator{
退货协调员(documentController:self)
}
func updateUIViewController(
_uiViewController:UIDocumentPickerViewController,
上下文:UIViewControllerRepresentableContext){
}
func makeUIViewController(上下文:context)->UIDocumentPickerViewController{
让控制器=UIDocumentPickerViewController(documentTypes:[字符串(kUTTypePDF)],位于:。打开)
controller.delegate=context.coordinator
返回控制器
}
类协调器:NSObject,UIDocumentPickerDelegate{
var documentController:DocumentPickerViewController
初始化(documentController:DocumentPickerViewController){
self.documentController=documentController
}
func documentPicker(uController:UIDocumentPickerViewController,didPickDocumentsAt URL:[URL]){
guard let url=url.first-else{return}
让fileManager=fileManager.default
打印(fileManager.fileExists(atPath:url.path))
let data=NSData(contentsOfFile:url.path)
让file=UploadFileData(文件名:“\(url)”,文件类型:。文件,文件数据:数据!)
让dataFile=file.fileData作为数据
documentController.callback(数据文件)
}
}
}
枚举上载文件类型{
案例照片
案例档案
}
结构上载文件数据{
变量文件名:字符串
var fileType:UploadFileType
var fileData:NSData
}
var文件:上传文件数据?
它在我的模拟器上工作,但当我在真实设备上选择PDF时,我会得到以下错误: 致命错误:在展开可选值时意外发现nil:文件MyApp/DocumentPickerViewController.swift,第44行


ie for line:
让dataFile=file.fileData作为数据

存在的问题是,在定义文档选择器使用的文件传输操作类型时,您使用了错误的模式
.open
用于打开位于应用程序沙箱外部的外部文件。您需要的是使用
.import
它将创建一个临时文件,允许您加载其内容或将文件移动/复制到永久位置。如果它不能解决您的问题,请检查如何实现DocumentPickerViewController协调器

let controller = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF)], in: .import)

问题在于,在定义文档选择器使用的文件传输操作类型时,您使用了错误的模式
.open
用于打开位于应用程序沙箱外部的外部文件。您需要的是使用
.import
它将创建一个临时文件,允许您加载其内容或将文件移动/复制到永久位置。如果它不能解决您的问题,请检查如何实现DocumentPickerViewController协调器

let controller = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF)], in: .import)