Search code examples
objective-ciphonesandboxios8ios-app-group

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?


Solution

  • 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