我想将令牌从服务器保存到 plist 中。我不确定是否必须创建一个 plist firs 或者它可以在我的 Document 目录中使用以下代码自动创建。但是,我无法创建一个 plist 并将我的字典写入其中。这是我的代码
-(void)writeToPlist:(NSString*)value forkey:(NSString *)key
{
NSLog(@"Write plist here");
//NSError *error;
NSArray *paths=NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory=[paths objectAtIndex:0];
NSString* path=[documentDirectory stringByAppendingFormat:@"Util.plist"];
NSLog(@"The path is %@",path);
NSFileManager *fileManager=[NSFileManager defaultManager];
NSMutableDictionary *data;
if(![fileManager fileExistsAtPath:path])
{
path=[[NSBundle mainBundle]pathForResource:@"Util" ofType:@"plist"];
}
[data setObject:value forKey:key];
[data writeToFile:path atomically:YES];//will it create the plist?
}