如题,模拟器和真机之间就文件的存储路径上还是有差别的,我遇到的问题如下:
当使用app路径存放时,真机不能永久存储文件,下次启动就没了,而模拟器即使在退出后下次还是能找到文件。
后来知道,文件存储的标准路径是document目录,例如:
补充:模拟器的路径是可以用mac的终端查看的,可是真机怎么查看就不知道了,如果有知道的达人麻烦告知我一下,谢谢。
真机:
/var/mobile/Applications/A1D90056-3DD0-4A4E-B679-487F656714CB/Documents/
模拟器:
/Users/drm/Library/Application Support/iPhone Simulator/5.1/Applications/0F0B2FDE-847A-4988-964A-746EC9313926/Documents/
而我使用的app路径是:
真机:
/var/mobile/Applications/A1D90056-3DD0-4A4E-B679-487F656714CB/***.app/
模拟器:
/Users/drm/Library/Application Support/iPhone Simulator/5.1/Applications/0F0B2FDE-847A-4988-964A-746EC9313926/***.app/
主要代码:
NSString *documentDirectory = [[NSBundle mainBundle]resourcePath];
NSLog(@"%@",[[NSBundle mainBundle]resourcePath]);
NSLog(@"%@",documentDirectory);
NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"myfile"];
NSLog(@"%@",filePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
[fileManager createFileAtPath:filePath contents:nil attributes:nil];
}
NSData *fileData = [NSData dataWithBytes:Ptr length:Size];
[fileData writeToFile:filePath atomically:YES];
NSLog(@"fileData = %@",fileData);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"document paths = %@",documentsDirectory);
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myfile"];
NSLog(@"%@",filePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
[fileManager createFileAtPath:filePath contents:nil attributes:nil];
}
NSData *fileData = [NSData dataWithBytes:Ptr length:Size];
[fileData writeToFile:filePath atomically:YES];
NSLog(@"save fileData = %@",fileData);