Accessing Files in iCloud Drive via Files app

I have an app which has defined a document type and is able to open such files with a given extension.


This works perfectly for example with AirDrop or from Mail, but it does not work with the new Files app in iOS 11

on a real device.


It works in the simulator, there are two cases: If the URL of the file to open comes in via


- (BOOL) application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options


I check if the file attributes are accessible. If so, I can use the file item directly, if not, I use


[NSFileCoordinator coordinateReadingItemAtURL:options:error:byAccessor:],


the file is downloaded from iCloud and I can process it using the new URL delivered by the result handler.


On a real device this does not work - I get an error that I am not having the access rights to access the file attributes.

If I am using the NSFileCoordinator, the result handler returns without an error, but trying to access the item at the new URL

leads to the same problem.


Does anybody have an idea what I can do to solve this?

Accepted Reply

The solution is to call


[NSURL startAccessingSecurityScopedResource / stopAccessingSecurityScopedResource]


before resp. after accessing the file at the new URL delivered by the NSFileCoordinator.

Replies

One addition: If I set LSSupportsOpeningDocumentsInPlace to NO in my info.plist,

the "old" behaviour is restored: If I select a file in the Files app, it is downloaded,

I see a screen with the filename on it and can provide the file to my app using the Share button.


But I would like to use LSSupportsOpeningDocumentsInPlace = YES to show

my document files in the Files app.


What do I have to do to handle this in-place-opening in my app in the right way?

The solution is to call


[NSURL startAccessingSecurityScopedResource / stopAccessingSecurityScopedResource]


before resp. after accessing the file at the new URL delivered by the NSFileCoordinator.

Hello!


I have an identical problem but I'm not sure how to apply your solution.


My app has also defined a document type and is able to open such files with Airdrop and Email. Now when I try to open a file from Files app/iCloud, the sahring menu appears, I select Open in my app, then my app opens but I cannot find the file.

When I did this via email, an Inbox folder was created inside my app with the file in it. But when I do this via Files app/iCloud there is no "icloud" folder or something like that, nor the file is in my app.

I've checked the device logs but I cannot find anything strange. And I cannot find any document that explains how to do this.


So, should I add something to my code (working with email and airdrop) so the files in Files app/iCloud can be opened/copied in my app?