當你遠程開發(fā)的時候,你寫的程序在自己的手機上沒問題,在幾千公里外的測試手機上有問題,看現(xiàn)象也看不出來啥問題。那可以把日志寫到手機里,然后導出來供你排查。
還有就是,偶發(fā)的一些問題,數(shù)據(jù)異常也可以寫入到本地,排查。
1.第一步,允許手機共享文件 這個必須加,不然你連接iTunes 看不到應用程序,沒法導出日志
在info.plist文件里添加一項
Application supports iTunes file sharing??????? 值設置成 YES

2. 在appdelegate 里調(diào)用以下方法
- (void)redirectNSLogToDocumentFolder
{
? ? //如果已經(jīng)連接Xcode調(diào)試則不輸出到文件
? ? 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 導數(shù)據(jù)

結束。