I try to create a Document Picker for my iOS app.
Here is my code (I wrapped the UIDocumentPickerViewController in my SwiftUI View, with UIViewControllerRepresentable):
import SwiftUI
import MobileCoreServices
struct DocumentPickerViewController: UIViewControllerRepresentable {
var callback: (Data) -> ()
func makeCoordinator() -> Coordinator {
return Coordinator(documentController: self)
}
func updateUIViewController(
_ uiViewController: UIDocumentPickerViewController,
context: UIViewControllerRepresentableContext<DocumentPickerViewController>) {
}
func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
let controller = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF)], in: .open)
controller.delegate = context.coordinator
return controller
}
class Coordinator: NSObject, UIDocumentPickerDelegate {
var documentController: DocumentPickerViewController
init(documentController: DocumentPickerViewController) {
self.documentController = documentController
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let url = urls.first else { return }
let fileManager = FileManager.default
print(fileManager.fileExists(atPath: url.path))
let data = NSData(contentsOfFile: url.path)
let file = UploadFileData(fileName: "\(url)", fileType: .file, fileData: data!)
let dataFile = file.fileData as Data
documentController.callback(dataFile)
}
}
}
enum UploadFileType{
case photo
case file
}
struct UploadFileData {
var fileName: String
var fileType: UploadFileType
var fileData: NSData
}
var file: UploadFileData?
It works on my Simulator, but when I pick a PDF on a real device, I get the following error: Fatal error: Unexpectedly found nil while unwrapping an Optional value: file MyApp/DocumentPickerViewController.swift, line 44
ie for line: let dataFile = file.fileData as Data