Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot save file to App Groups on iOS 8

I tried to use - (NSURL *)containerURLForSecurityApplicationGroupIdentifier:(NSString *)groupIdentifier to save / load files between container app and extension on iOS 8 b4.

Here's the code:

NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:groupID];
containerURL = [containerURL URLByAppendingPathComponent:[NSString stringWithFormat:@"Library/Caches/test.txt"]];
NSString* string = @"hihihihihi";
NSError* error;
[string writeToURL:containerURL atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@",error);

I put this code in both the extension (custom keyboard) and container app. When I run it on iOS Simulator, both of them can save the file successfully. However on iOS Devices, the extension can't save the file (while container app still can)

The console shows cocoa error 513 when the extension tried to save file.

2014-07-31 01:37:49.316 TestExtention[2359:288820] Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x15df2100 {NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/4FA2DA5E-89C3-4BC3-AA5B-DD869827C5E7/Library/Caches/test.txt, NSUnderlyingError=0x15df1d90 "The operation couldn’t be completed. Operation not permitted"}

Is this a limitation of iOS 8 extension?

like image 335
Hiraku Avatar asked Jul 30 '14 18:07

Hiraku


People also ask

How do I save to files on iPhone?

When you want to save an item to Files, tap the Share icon (which looks like a box with an arrow on top). This opens the Share tab, next select Save to Files. You may have to tap the More icon (which has three dots on it) to select Save to Files.

How can I save files on iPad?

Files you create are saved automatically in the corresponding app folder on your iPad (accessible using the Apple Files app). Therefore, there's no need to explicitly save a file, unless you wish to save a copy elsewhere.


2 Answers

There's another restriction for keyboard extension. You should set RequestsOpenAccess to YES in Info.plist then it can access the files.

https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/Keyboard.html#//apple_ref/doc/uid/TP40014214-CH16-SW2

like image 178
Hiraku Avatar answered Sep 25 '22 07:09

Hiraku


Both the containing app and the extension can access files under the same shared group container, if they have same group id.

In both of your containing app and extension, check whether this exists in it's .entitlements.

<key>com.apple.security.application-groups</key>
<array>
    <string>your-group-id</string>
</array>


Then check their Provisioning Profile has this group id.

like image 37
user3876330 Avatar answered Sep 23 '22 07:09

user3876330