1. 程式人生 > >iOS Crash閃退日誌獲取和上傳至伺服器

iOS Crash閃退日誌獲取和上傳至伺服器

 如何獲取crash閃退日誌 -- 工具檢視

        先看第一個問題如何檢視,我搜索的方法有以下幾個:

        第一個方法:XCode  的選單Window->Organizer    選擇Devices  ->  選中的手機 -> 點選手機名稱左邊的箭頭 會等到如下圖

注意對比一下紅色框框內容,這個日誌也基本上上告訴你crash的原因了。

       第二種方法 開啟手機 - > 設定 -> 隱私 - > 診斷與用量 - > 診斷與用量資料  這裡面就是所有應用的Crash日誌。

       第三種方法 通過iTunes Connect(Manage Your Applications - View Details - Crash Reports)獲取使用者的crash日誌。方法很多這裡不多列了。

      解析crash

      參見:http://stackoverflow.com/questions/1460892/symbolicating-iphone-app-crash-reports )

      用程式獲取crash日誌

      但是這裡都是工具,沒有用到程式獲取,經過千方百計的查詢(思路是:先找到存放crash的iphone系統路徑:var/mobile/Library/Logs/CrashReporter)找到了crash存放的路徑,唉,苦於無法讀取(用程式讀出來都是nil),當然如果是越獄手機就不一樣是可以讀取的。這個思路斷掉了。

       換個思路:自己用程式捕獲crash,儲存到本地可以嗎?這麼一試,果然........

       第一步:新建一個繼承自NSObject的類(Xcode新建一個空專案過程略),取名字CatchCrash,在h和m檔案中寫下:

 .h檔案

  1. #import <Foundation/Foundation.h>

  2. @interface CatchCrash : NSObject  

  3. void uncaughtExceptionHandler(NSException *exception);  

  4. @end 

.m檔案

  1. #import "CatchCrash.h"  

  2. @implementation CatchCrash  

  3. void uncaughtExceptionHandler(NSException *exception)  

  4. {  

  5.     // 異常的堆疊資訊  

  6.     NSArray *stackArray = [exception callStackSymbols];  

  7.     // 出現異常的原因  

  8.     NSString *reason = [exception reason];  

  9.     // 異常名稱  

  10.     NSString *name = [exception name];  

  11.     NSString *exceptionInfo = [NSString stringWithFormat:@"Exception reason:%@\nException name:%@\nException stack:%@",name, reason, stackArray];  

  12.     NSLog(@"%@", exceptionInfo);  

  13.     NSMutableArray *tmpArr = [NSMutableArray arrayWithArray:stackArray];  

  14.     [tmpArr insertObject:reason atIndex:0];  

  15.     //儲存到本地  --  當然你可以在下次啟動的時候,上傳這個log  

  16.     [exceptionInfo writeToFile:[NSString stringWithFormat:@"%@/Documents/error.log",NSHomeDirectory()]  atomically:YES encoding:NSUTF8StringEncoding error:nil];  

  17. }  

  18. @end  

第二步:新增一個繼承自UIViewcontroller的類,取名字為TestViewController。

第三步:註冊CatchCrash異常處理方法,在Appdelegate寫下如下程式碼:

  1. #import "AppDelegate.h"  

  2. #import "CatchCrash.h"  

  3. #import "TestViewController.h"  

  4. @implementation AppDelegate  

  5. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  

  6. {  

  7.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  

  8.     // Override point for customization after application launch.  

  9.     //註冊訊息處理函式的處理方法  

  10.     NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);  

  11.     TestViewController *testVc = [[TestViewController alloc] init];  

  12.     self.window.rootViewController = testVc;  

  13.     self.window.backgroundColor = [UIColor whiteColor];  

  14.     [self.window makeKeyAndVisible];  

  15.     return YES;  

  16. }  


       第四部:在TestViewController的Xib上面新增一個按鈕並給其新增一個單擊事件,TestViewController.m檔案中有如下程式碼:

  1. #import "TestViewController.h"  

  2. @interface TestViewController ()  

  3. @end  

  4. @implementation TestViewController  

  5. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  

  6. {  

  7.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  

  8.     if (self) {  

  9.         // Custom initialization  

  10.     }  

  11.     return self;  

  12. }  

  13. - (void)viewDidLoad  

  14. {  

  15.     [super viewDidLoad];  

  16.     // Do any additional setup after loading the view from its nib.  

  17. }  

  18. - (void)didReceiveMemoryWarning  

  19. {  

  20.     [super didReceiveMemoryWarning];  

  21.     // Dispose of any resources that can be recreated.  

  22. }  

  23. #pragma mark - 單擊事件  

  24. - (IBAction)crashTapped:(id)sender  

  25. {  

  26.     //常見異常1---不存在方法引用  

  27. //    [self performSelector:@selector(thisMthodDoesNotExist) withObject:nil];  

  28.     //常見異常2---鍵值對引用nil  

  29. //    [[NSMutableDictionary dictionary] setObject:nil forKey:@"nil"];  

  30.     //常見異常3---陣列越界  

  31.     [[NSArray array] objectAtIndex:1];  

  32.     //常見異常4---memory warning 級別3以上  

  33. //    [self performSelector:@selector(killMemory) withObject:nil];  

  34.     //其他大家去想吧  

  35. }  

  36. #pragma mark - custom method  

  37. - (void) killMemory  

  38. {  

  39.     for (int i = 0; i <300; i ++)  

  40.     {  

  41.         UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];  

  42.         tmpLabel.layer.masksToBounds = YES;  

  43.         tmpLabel.layer.cornerRadius = 10;  

  44.         tmpLabel.backgroundColor = [UIColor redColor];  

  45.         [self.view addSubview:tmpLabel];  

  46.     }  

  47. }  

  48. @end  

執行程式碼:可以看到閃退,匯出error日誌,我們可以看到:

  1. Exception reason:NSRangeException  

  2. <spanstyle="color:#FF0000;">Exception name:*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds for empty array</span>

  3. Exception stack:(  

  4.     0   CoreFoundation                      0x2f2edfeb <redacted> + 154  

  5.     1   libobjc.A.dylib                     0x39b66ccf objc_exception_throw + 38  

  6.     2   CoreFoundation                      0x2f224a89 <redacted> + 176  

  7. <spanstyle="color:#FF0000;"> 3   TestCrash                           0x000e8077 -[TestViewController crashTapped:] + 126</span>

  8.     4   UIKit                               0x31b3f057 <redacted> + 90  

  9.     5   UIKit                               0x31b3eff7 <redacted> + 30  

  10.     6   UIKit                               0x31b3efd1 <redacted> + 44  

  11.     7   UIKit                               0x31b2a737 <redacted> + 374  

  12.     8   UIKit                               0x31b3ea4f <redacted> + 590  

  13.     9   UIKit                               0x31b3e721 <redacted> + 528  

  14.     10  UIKit                               0x31b396eb <redacted> + 758  

  15.     11  UIKit                               0x31b0e8ed <redacted> + 196  

  16.     12  UIKit                               0x31b0cf97 <redacted> + 7102  

  17.     13  CoreFoundation                      0x2f2b925b <redacted> + 14  

  18.     14  CoreFoundation                      0x2f2b872b <redacted> + 206  

  19.     15  CoreFoundation                      0x2f2b6f1f <redacted> + 622  

  20.     16  CoreFoundation                      0x2f221f0f CFRunLoopRunSpecific + 522  

  21.     17  CoreFoundation                      0x2f221cf3 CFRunLoopRunInMode + 106  

  22.     18  GraphicsServices                    0x3417a663 GSEventRunModal + 138  

  23.     19  UIKit                               0x31b6d16d UIApplicationMain + 1136  

  24.     20  TestCrash                           0x000e810d main + 116  

  25.     21  libdyld.dylib                       0x3a073ab7 <redacted> + 2  

  26. )