我已将以下内容添加到Cordova.plist文件中:
Key: EnableEverything
Type: Boolean
Value: YES
然后我使用以下代码来读取和写入plist文件:
NSString *adKey = @"EnableEverything";
NSString *pathToSettingsInBundle = [[NSBundle mainBundle] pathForResource:@"Cordova" ofType:@"plist"];
NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: pathToSettingsInBundle];
NSString *enableEverything = [[plist valueForKey:adKey] stringValue];
NSLog(@"EnableEverything: %@", enableEverything); // this returns 1, which is correct.
// Disable in plist.
[plist setValue:NO forKey:adKey];
[plist writeToFile:pathToSettingsInBundle atomically:YES];
NSLog(@"EnableEverything: %@", enableEverything); // never reaches this line
我收到以下错误:
2012-09-07 14:20:12.168 slq[958:707] *** WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: <NSInternalInconsistencyException> -[__NSCFDictionary removeObjectForKey:]: mutating method sent to immutable object
当它试图写入文件时似乎有问题。
答案 0 :(得分:1)
您正在更改应用包中的文件。这是苹果禁止的。检查一下:writing NSDictionary to plist in my app bundle
您必须将plist复制到允许更改的文件夹,例如documents文件夹。