iOS 9错误写.plist

时间:2015-09-30 03:11:29

标签: objective-c plist ios9

更新到iOS 9后我的应用程序错误。无法在项目中编写.plist文件。

-(BOOL)writeDictionaryToPlist:(NSDictionary*)plistDict {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"History" ofType:@"plist"];

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValuesForKeysWithDictionary:plistDict];

    NSDictionary *saveDict = [NSDictionary dictionaryWithDictionary:dict];
    BOOL result = [saveDict writeToFile:path atomically:YES];

    return result;
}

resultNO

但iOS 7和iOS 8工作正常。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

使用dictionaryWithContentsOfFile:从plist写入字典

NSString *path = [[NSBundle mainBundle] pathForResource:@"History" ofType:@"plist"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
BOOL result = [dict writeToFile:path atomically:YES];

注意,因为plist是您无法写入该文件的应用资源,因此会在您的文档目录中创建一个plist文件并写入该文件。

NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
path = [path stringByAppendingString:@"/History.plist"];
BOOL result = [dict writeToFile:path atomically:YES];

从plist

中检索数据
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:path];

<强>更新 将NSAppTransportSecurity添加为字典,第一个子节点应为名为NSAllowsArbitraryLoads的bool设置为YES。

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>