当你远程开发的时候,你写的程序在自己的手机上没问题,在几千公里外的测试手机上有问题,看现象也看不出来啥问题。那可以把日志写到手机里,然后导出来供你排查。
还有就是,偶发的一些问题,数据异常也可以写入到本地,排查。
1.第一步,允许手机共享文件 这个必须加,不然你连接iTunes 看不到应用程序,没法导出日志
在info.plist文件里添加一项
Application supports iTunes file sharing 值设置成 YES

2. 在appdelegate 里调用以下方法
- (void)redirectNSLogToDocumentFolder
{
//如果已经连接Xcode调试则不输出到文件
if(isatty(STDOUT_FILENO)) {
return;
}
UIDevice *device = [UIDevice currentDevice];
if([[device model] hasSuffix:@"Simulator"]){ //在模拟器不保存到文件中
return;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"apptest.log"];
NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];
// Delete existing files
NSFileManager *defaultManager = [NSFileManager defaultManager];
[defaultManager removeItemAtPath:logFilePath error:nil];
//Enter the log into the file
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
}
3.第三步,相应的地方用代码 nslog 打印输出。(当然也可以把项目闪退的异常抛错也能写进去)
4. 第四步,连上手机 打开iTunes 导数据

结束。
网友评论