iOS通过Plist保存离线调试日志

最近需要测试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/),就可以查看真机的文件系统,如图:




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值