最近需要测试APP在iPhone没连接USB情况下定位时间间隔的情况,固把nslog的日志信息保存成本地Plist文件,以便测试结束后查阅运行时的日志。
一、新建一个保存日志的方法,参数为每次定位成功的时间(作为key),定位成功的地点(作为value):
-(void)mylog:(NSString*)logKey andValue:(NSString*)logValue{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectroy = [paths objectAtIndex:0];
NSString *filename =@"test2.plist";
NSString *filePath = [documentsDirectroy stringByAppendingPathComponent:filename];
NSLog(@"filePath:%@",filePath);
NSFileManager *file = [NSFileManager defaultManager];
NSMutableDictionary *dic;
if ([file fileExistsAtPath:filePath]) {
dic = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}else{
dic = [[NSMutableDictionary alloc] init];
}
[dic setValue:logValue forKey:logKey];
[dic writeToFile:filePath atomically:YES];
}
二、在定位成功时调用mylog函数,以保存定位成功的信息:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
nowLocation = [locations lastObject];
CLLocation *locationGCJ = [nowLocation locationMarsFromEarth];
coordinate = locationGCJ.coordinate;
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:locationGCJ completionHandler:^(NSArray *placemarks, NSError *error) {
if(placemarks.count > 0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
address =placemark.name;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *destDateString = [dateFormatter stringFromDate:[NSDate date]];
[self mylog2:destDateString andValue:placemark.name];
}
}];
}
三、分别让APP在模拟器和真机跑了一段时间后,我们来查看下保存的日志信息:
1、模拟器:通过my log函数里打印的文件地址 (我的为:filePath:/Users/jackxu/Library/Developer/CoreSimulator/Devices/B24F5E52-CB0D-41DF-8623-8CEDF21206FC/data/Containers/Data/Application/F61B5D71-1BD5-49D6-8B32-628E03434A8C/Documents/test2.plist),选择finder->前往->前往文件夹,输入地址就行可访问,如图:
2.真机:真机下载一个 iPhone Explorer,(下载地址:http://www.macroplant.com/iexplorer/),就可以查看真机的文件系统,如图: