Document Picker does not work on device

I have a problem with Xcode version 15.0.1: I have programmed a DocumentPicker in Swift UI to load a document from my own cloud into the app. When I run the app, it works fantastic with the simulator, but it doesn't work with my device. I always get the following error message: file is not reachable. Ensure file URL is not a directory, symbolic link, or invalid url. As I said, when I run the code in the simulator, it works fine. I am using IOS 17.2 Here is the code.

import SwiftUI import MobileCoreServices import Firebase import FirebaseStorage import CoreData import UniformTypeIdentifiers struct DocumentPickerView: UIViewControllerRepresentable { @Binding var fileContent : String @Binding var nameContent : String @Binding var showqr : Bool @Binding var documentURL: URL? @Binding var alert: Bool

//  @Binding var webViewURL : URL

func makeCoordinator() -> Coordinator {
    Coordinator(self)
}

func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
   // let picker = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import)
    let types: [UTType] = [UTType.content]
   let picker = UIDocumentPickerViewController(forOpeningContentTypes:types)
    picker.allowsMultipleSelection = false
    picker.delegate = context.coordinator
    return picker
}

func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) {}

class Coordinator: NSObject, UIDocumentPickerDelegate {
    var parent: DocumentPickerView
    
    init(_ parent: DocumentPickerView) {
        self.parent = parent
    }
    
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        if let selectedURL = urls.first {
            parent.documentURL = selectedURL
            parent.uploadToFirebase(selectedURL)
        }
    }
}

func uploadToFirebase(_ fileURL: URL) {
    
    let storage = Storage.storage()
    let storageRef = storage.reference()
    let fileRef = storageRef.child(fileURL.lastPathComponent)
  //  if FileManager.default.fileExists(atPath: fileURL.path){
  //     print("File Existiert:\(fileURL)")
  //  } else {
   //     print("File Existiert nicht")
   // }
    _ = fileRef.putFile(from: fileURL, metadata: nil){ (metadata, error) in
    
        if let error = error {
            print("Fehler beim Hochladen der Datei: \(error.localizedDescription)")
        } else {
            
            print("Datei erfolgreich hochgeladen!")
            
            // Rufen Sie die Download-URL für die hochgeladene Datei ab
            fileRef.downloadURL { (url, error) in
                if let error = error {
                    alert = true
                    print("Fehler beim Abrufen der Download-URL: \(error.localizedDescription)")
                } else {
                    if let downloadURL = url {
                        print("Download-URL: \(downloadURL)")
                        self.fileContent = "\(downloadURL)"
                        self.showqr = true
                    }
                    
                    
                }
                
            }
            
        }
    }
}

}

There are three moving parts here:

  • SwiftUI

  • UIKit

  • The file system

I suggest you try to eliminate one of them, so either:

  • Write a tiny test project that uses UIKit directly, rather than via a SwiftUI wrapper.

  • Call SwiftUI’s document picker directly.

I always get the following error message: file is not reachable.

What API generates that error?

What are the details of those error? If you convert it to an NSError, what do you see in the domain and code properties? And does the userInfo property have any details of the underlying error?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

On device your App runs in the sandbox, in the simulator there are no file access restrictions:

You have to request access to files in certain directories using code similar to this:

 _ = url.startAccessingSecurityScopedResource()

// your file access here

url.stopAccessingSecurityScopedResource()
           

https://developer.apple.com/documentation/foundation/nsurl/1417051-startaccessingsecurityscopedreso

Document Picker does not work on device
 
 
Q