iOS Crash常規跟蹤方法及Bugly整合運用詳細介紹_IOS

來源:互聯網
上載者:User

iOS Crash常規跟蹤方法及Bugly整合運用

當app出現崩潰, 研發階段一般可以通過以下方式來跟蹤crash資訊

#1.模擬器運行, 查看xcode錯誤記錄檔

#2.真機調試, 查看xcode錯誤記錄檔

#3.真機運行, 查看device系統日誌

 下面舉例說明, 先寫一段會Crash的代碼crashdemo:

- (void)viewDidLoad {  [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.  [self performSelector:@selector(print) withObject:nil afterDelay:5];}- (void)print {  NSArray *array = @[];  NSLog(@"%@", array[1]);}

Demo#1.模擬器運行, 查看xcode錯誤記錄檔

程式執行後會立即崩潰, 開啟xcode系統日誌可以看到以下錯誤資訊

2016-10-29 12:13:29.015 CrashDemo[37842:7436441] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 1 beyond bounds for empty NSArray'*** First throw call stack:(  0  CoreFoundation           0x00b7ba84 __exceptionPreprocess + 180  1  libobjc.A.dylib           0x00642e02 objc_exception_throw + 50  2  CoreFoundation           0x00b22390 __CFArrayGetTypeID_block_invoke + 0  3  CoreFoundation           0x00ac07f8 -[NSArray objectAtIndexedSubscript:] + 40  4  CrashDemo              0x000877b7 -[ViewController print] + 87  5  Foundation             0x00250d71 __NSFireDelayedPerform + 442  6  CoreFoundation           0x00acd576 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22  7  CoreFoundation           0x00accf72 __CFRunLoopDoTimer + 1250  8  CoreFoundation           0x00a8b25a __CFRunLoopRun + 2202  9  CoreFoundation           0x00a8a706 CFRunLoopRunSpecific + 470  10 CoreFoundation           0x00a8a51b CFRunLoopRunInMode + 123  11 GraphicsServices          0x041e4664 GSEventRunModal + 192  12 GraphicsServices          0x041e44a1 GSEventRun + 104  13 UIKit                0x00f0c1eb UIApplicationMain + 160  14 CrashDemo              0x00087bba main + 138  15 libdyld.dylib            0x03189a21 start + 1)libc++abi.dylib: terminating with uncaught exception of type NSException(lldb) 

通過xcode日誌可以看到是數組訪問越界, 發生越界的方式名為print

針對這個demo我們當然很清楚是剛才列的array[1]發生越界, 但對於一個完整的程式如何查看是在哪個地方發生越界的呢?

這個時候我們可以利用xcode的Show the breakpoint navigator功能, 點加號選擇add exception breakpoint

這個時候我們在執行程式, xcode執行會自動停在要發生crash的程式碼片段

Demo#2.真機調試, 查看xcode錯誤記錄檔
如果有添加exeception point, 程式會自動停到列印array[1]那一行. 如果沒有添加則程式會crash, xcode會出現以下錯誤記錄檔

2016-10-29 12:15:53.561 CrashDemo[1062:316582] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 1 beyond bounds for empty NSArray'*** First throw call stack:(0x211b398b 0x2094ee17 0x211433e7 0xc5a3b 0x219d1ad5 0x211765ff 0x21176231 0x2117407d 0x210c32e9 0x210c30d5 0x226b3ac9 0x257880b9 0xc5c99 0x20d6b873)libc++abi.dylib: terminating with uncaught exception of type NSException(lldb) 

通過錯誤資訊我們只能看到是有發生數組訪問越界, 如果有添加exeception breakpoint則會自動停在發生error的程式碼.

 Demo#3. 真機運行, 查看device系統日誌

xcode停止運行這個crashdemo, 選擇xcode window - devices, 選擇手機 - view device logs

然後在手機上運行crashdemo, 在device logs中按時間排序查看最新的log就能看到crashdemo的crash log

Incident Identifier: 9A4C52F0-B0D7-42C9-A7CB-D4D3321D00D5CrashReporter Key:  90f4d3621773443794fa73f506fd6bdef49fc269Hardware Model:   iPhone4,1Process:       CrashDemo [1074]Path:        /private/var/containers/Bundle/Application/1307034E-9C2B-451F-ACD9-04C97DEC047B/CrashDemo.app/CrashDemoIdentifier:     PEGA.CrashDemoVersion:       1 (1.0)Code Type:      ARM (Native)Parent Process:   launchd [1]Date/Time:      2016-10-29 12:21:49.49 +0800Launch Time:     2016-10-29 12:21:43.43 +0800OS Version:     iOS 9.3.1 (13E238)Report Version:   104Exception Type: EXC_CRASH (SIGABRT)Exception Codes: 0x0000000000000000, 0x0000000000000000Exception Note: EXC_CORPSE_NOTIFYTriggered by Thread: 0Filtered syslog:None foundLast Exception Backtrace:0  CoreFoundation          0x211b3986 __exceptionPreprocess + 1221  libobjc.A.dylib          0x2094ee12 objc_exception_throw + 342  CoreFoundation          0x211433e2 -[__NSArray0 objectAtIndex:] + 1103  CrashDemo             0x000e6a36 0xe0000 + 271904  Foundation            0x219d1ad0 __NSFireDelayedPerform + 4645  CoreFoundation          0x211765fa 

這些在開發階段都能很簡便的實現, 但是當app發布出去後使用者發生crash呢? 一般使用者只能反饋在做什麼的時候發生crash

然後我們在去做嘗試是否能遇到, 不過這樣效率不高而且一般很難複現到使用者的crash

Bugly的出現解決的這個問題

Bugly SDK在當程式崩潰時, 會自動將錯誤資訊發送到伺服器方便開發人員查看分析

那麼如何使用Bugly?

首先先到https://bugly.qq.com/v2/註冊帳號, 並註冊app下載SDK包

將Bugly.framework拖拽到工程中, 記得勾選copy if needed.

然後添加libz.tbd / libstdc++.tbd / Security.framework / SystemConfiguration.framework到工程中

delegate.m中註冊

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     [Bugly startWithAppId:@"此處替換為你的AppId"];     return YES;  }

這樣當程式發生崩潰時, 崩潰資訊會自動發送到伺服器登入你的bugly帳號就能查看到了

 

 感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.