我想將來自服務器的令牌保存到plist中。我不確定如果我必須創建一個plist冷杉,或者它可以自動使用我的Document directroy中的以下代碼創建。但是,我無法創建一個plist並在其中寫入我的字典。 這裏是我的代碼iOS:讀寫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?
}