我尝试使用 - (NSURL *)containerURLForSecurityApplicationGroupIdentifier:(NSString *)groupIdentifier
在 iOS 8 b4 上的容器应用程序和扩展程序之间保存/加载文件。
代码如下:
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);
我将此代码放在扩展(自定义键盘)和容器应用程序中。 当我在 iOS 模拟器上运行时,它们都可以成功保存文件。但是在 iOS 设备上,扩展无法保存文件(而容器应用程序仍然可以)
当扩展试图保存文件时,控制台显示 cocoa 错误 513。
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"}
这是 iOS 8 扩展的限制吗?
最佳答案
键盘扩展还有另一个限制。您应该在 Info.plist 中将 RequestsOpenAccess
设置为 YES
然后它就可以访问文件了。
关于objective-c - 无法将文件保存到 iOS 8 上的应用程序组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25043771/