Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot open file from iOS Files app via the Open In <My App> share sheet: no file at the provided file URL exists

My iOS app can open CSV files, in order to import their data. I can open files from within the app via a UIDocumentPickerViewController with no issues, selecting a file shown in the Files app. However, when viewing a file in the Files app first, and then opening my app from there (via the Open In share sheet), my app cannot see the file at the URL passed to my app. The file does not seem to exist.

I added the following debug code to my AppDelegate:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
    print("Exists: \(FileManager.default.fileExists(atPath: url.path)) (\(url.path))")
    return true
}

When I open a file from Files in this app, it results in a log line like:

Exists: false (/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/Reading List - Andrew’s iPhone - 2018-10-18 11-19.csv)

When I open a file from some other app (e.g. Dropbox, or from an Email), the file can be processed, and the following is logged:

Exists: true (/private/var/mobile/Containers/Data/Application/F9110F90-7A91-4AB6-A92E-0ED933184EA4/Documents/Inbox/Reading List - Andrew’s iPhone - 2018-01-27 08-03.csv)

Note the different path (Documents/Inbox vs Mobile Documents/com~apple~CloudDocs). What is causing this? How can I support the opening of files from the Files app in my app?

The document type(s) supported by my app are as follows:

Xcode document types

like image 602
Andrew Bennet Avatar asked Jan 30 '19 23:01

Andrew Bennet


People also ask

Why can't I open my files on iPhone?

To resolve the issue, you need to first check that the Files app has the required permissions to use cellular data. On the Settings screen, tap Cellular Data, scroll down, and then check that the switch next to Files is set to On. If you found it disabled, just turn it back on and you've fixed the problem already.

How do I access network files in IOS app?

In the IIS Manager , select the Application Pool under which your Web Site is running. Click "Advanced Settings". There will be an entry for Identity (it is under the Process Model section). Click it, provide credentials for your account that has permission to access the share.

How do I enable app files on my iPhone?

Connect Services to the Files App The first time you launch Files, you'll need to enable and connect to the services you use. Tap the ellipsis icon at the top and select Edit from the menu. The app displays all the available file storage services. Turn on the switch for any online locations you want to add.

How do I view open files on iPhone?

Browse and open files and foldersTap Browse at the bottom of the screen, then tap an item on the Browse screen. If you don't see the Browse screen, tap Browse again. To view recently opened files, tap Recents at the bottom of the screen. To open a file, location, or folder, tap it.


1 Answers

Setting the LSSupportsOpeningDocumentsInPlace flag to false prevents the app to be automatically opened when selecting the file in the Files App. In case the the file is opened in place one needs to request/unlock access first:

 func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {     
   let opensInPlace = options[.openInPlace] != nil
   opensInPlace ? url.startAccessingSecurityScopedResource() : nil
   let fileExists = FileManager.default.fileExists(atPath: url.path)  
   opensInPlace ? url.stopAccessingSecurityScopedResource() : nil
 }
like image 127
crom87 Avatar answered Sep 28 '22 22:09

crom87