URL in scene openURLContexts does not exist

I'm trying to update an old iOS app to use the UIScene architecture, and I've run into a problem I'm hoping somebody can help me fix.

When I try to share a file with my app by AirDrop from another device, the URL that I'm getting from the urlContexts argument does not exist on the device.

Here's the code I'm using in my SceneDelegate class to get things going:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else {
        os_log("URL in %@ is nil", #function)
        return
    }

    // check that it's the proper file type
    guard url.pathExtension == fileExtension.replacingOccurrences(of: ".", with: "") else {
        return
    }
    ...

}

When I set a breakpoint after populating the url property, checking its existence in the Xcode Console reveals the enclosing folder does not exist:

(lldb) po url.path
/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/Downloads/Seattle Times 2025-04-30 10.dbsud
(lldb) po FileManager.default.fileExists(atPath: URL(filePath: "/private/var/mobile/Library/Mobile Documents").path)
true
(lldb) po FileManager.default.fileExists(atPath: URL(filePath: "/private/var/mobile/Library/Mobile Documents/com-apple-CloudDocs/Downloads").path)
false
(lldb) po FileManager.default.fileExists(atPath: URL(filePath: "/private/var/mobile/Library/Mobile Documents/com-apple-CloudDocs/").path)
false

So when I try to process the file at the provided URL, it fails of course. Any ideas on how I can fix this?

This is happening while running the code on an iPad with iOS 18.5 from Xcode 16.4. If it makes a difference, the AirDrop is originated from an iPhone 16 Pro also running iOS 18.5.

Your tests have typos. The value of url.path includes com~apple~CloudDoc. Those are tildes, not dashes.

Also note that the URL based to openURLContexts is only valid while that method is being executed. Once it returns, the URL is no longer valid.

Thanks for pointing out my error. Unfortunately I'm still getting the same results with the correct separator:

(lldb) po FileManager.default.fileExists(atPath: "/private/var/mobile/Library/Mobile Documents/")
true
(lldb) po FileManager.default.fileExists(atPath: "/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs")
false
(lldb) po FileManager.default.fileExists(atPath: url.path)
false

To try to determine that this wasn't just a LLDB issue, I updated my code slightly:

    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        guard let url = URLContexts.first?.url else {
            os_log("URL in %@ is nil", #function)
            return
        }
        
        let fileExists = FileManager.default.fileExists(atPath: url.path)        

fileExists is false after AirDropping a file.

And of course this is happening within the method where openURLContexts is passed as an argument, and therefore the URL should be valid.

So I'm still stuck at the same place.

URL in scene openURLContexts does not exist
 
 
Q