Wil*_*d89 3 xcode ios ios-simulator
-(void)login{
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"login" ofType:@"plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
[plistDict setObject:@"si" forKey:@"stato"];
[plistDict writeToFile:path atomically: YES];
}
Run Code Online (Sandbox Code Playgroud)
在iOS模拟器中,plist已经正确编写,但是当我尝试在我的iPhone上编写.plist时,它不起作用.我想这是因为错误的.plist路径.iOS设备使用不同的路径吗?
首先,您必须检查文件是否退出文档目录.如果它没有退出,那么您可以将其复制到文档目录.你可以这样做
-(void)login{
BOOL doesExist;
NSError *error;
NSString *filePath= [[NSBundle mainBundle] pathForResource:@"login" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * path =[[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"login.plist"]];
doesExist= [fileManager fileExistsAtPath:path];
if (doesExist) {
NSMutableDictionary* plistDict=[[NSMutableDictionary alloc] initWithContentsOfFile:path];
}
else
{
doesExist= [fileManager copyItemAtPath:filePath toPath:path error:&error];
NSMutableDictionary* plistDict=[[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}
[plistDict setObject:@"si" forKey:@"stato"];
[plistDict writeToFile:path atomically: YES];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3518 次 |
| 最近记录: |