objective-c - 无法将文件保存到 iOS 8 上的应用程序组

标签 objective-c iphone sandbox ios8 ios-app-group

我尝试使用 - (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 然后它就可以访问文件了。

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

关于objective-c - 无法将文件保存到 iOS 8 上的应用程序组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25043771/

相关文章:

c# - 如何在 C# 中为外部进程创建沙箱?

iphone - iOS Scrollview 检测滚动

iphone - 尝试将 TextField 保存在 TableView 中

javascript - all_frames :true not working for stackoverflow snippet output window

python - Linux 中的安全 Python 环境

iphone - Xcode目标架构

ios - Xcode 运行成功但归档时出错

ios - 如何在 Objective C 的 UI 导航栏上添加很棒的字体

iphone - 从 iPhone 应用程序链接到 iBook 商店中的一本书

iphone - 如何让scrollview内容循环播放?