iOS Crash常規(guī)跟蹤方法及Bugly集成運(yùn)用詳細(xì)介紹
iOS Crash常規(guī)跟蹤方法及Bugly集成運(yùn)用
當(dāng)app出現(xiàn)崩潰, 研發(fā)階段一般可以通過以下方式來跟蹤crash信息
#1.模擬器運(yùn)行, 查看xcode錯誤日志
#2.真機(jī)調(diào)試, 查看xcode錯誤日志
#3.真機(jī)運(yùn)行, 查看device系統(tǒng)日志
下面舉例說明, 先寫一段會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.模擬器運(yùn)行, 查看xcode錯誤日志
程序執(zhí)行后會立即崩潰, 打開xcode系統(tǒng)日志可以看到以下錯誤信息
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日志可以看到是數(shù)組訪問越界, 發(fā)生越界的方式名為print
針對這個demo我們當(dāng)然很清楚是剛才列的array[1]發(fā)生越界, 但對于一個完整的程序如何查看是在哪個地方發(fā)生越界的呢?
這個時候我們可以利用xcode的Show the breakpoint navigator功能, 點(diǎn)加號選擇add exception breakpoint
這個時候我們在執(zhí)行程序, xcode執(zhí)行會自動停在要發(fā)生crash的代碼段
Demo#2.真機(jī)調(diào)試, 查看xcode錯誤日志
如果有添加exeception point, 程序會自動停到打印array[1]那一行. 如果沒有添加則程序會crash, xcode會出現(xiàn)以下錯誤日志
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)
通過錯誤信息我們只能看到是有發(fā)生數(shù)組訪問越界, 如果有添加exeception breakpoint則會自動停在發(fā)生error的代碼行.
Demo#3. 真機(jī)運(yùn)行, 查看device系統(tǒng)日志
xcode停止運(yùn)行這個crashdemo, 選擇xcode window - devices, 選擇手機(jī) - view device logs
然后在手機(jī)上運(yùn)行crashdemo, 在device logs中按時間排序查看最新的log就能看到crashdemo的crash log
Incident Identifier: 9A4C52F0-B0D7-42C9-A7CB-D4D3321D00D5 CrashReporter Key: 90f4d3621773443794fa73f506fd6bdef49fc269 Hardware Model: iPhone4,1 Process: CrashDemo [1074] Path: /private/var/containers/Bundle/Application/1307034E-9C2B-451F-ACD9-04C97DEC047B/CrashDemo.app/CrashDemo Identifier: PEGA.CrashDemo Version: 1 (1.0) Code Type: ARM (Native) Parent Process: launchd [1] Date/Time: 2016-10-29 12:21:49.49 +0800 Launch Time: 2016-10-29 12:21:43.43 +0800 OS Version: iOS 9.3.1 (13E238) Report Version: 104 Exception Type: EXC_CRASH (SIGABRT) Exception Codes: 0x0000000000000000, 0x0000000000000000 Exception Note: EXC_CORPSE_NOTIFY Triggered by Thread: 0 Filtered syslog: None found Last Exception Backtrace: 0 CoreFoundation 0x211b3986 __exceptionPreprocess + 122 1 libobjc.A.dylib 0x2094ee12 objc_exception_throw + 34 2 CoreFoundation 0x211433e2 -[__NSArray0 objectAtIndex:] + 110 3 CrashDemo 0x000e6a36 0xe0000 + 27190 4 Foundation 0x219d1ad0 __NSFireDelayedPerform + 464 5 CoreFoundation 0x211765fa
這些在開發(fā)階段都能很簡便的實現(xiàn), 但是當(dāng)app發(fā)布出去后用戶發(fā)生crash呢? 一般用戶只能反饋在做什么的時候發(fā)生crash
然后我們在去做嘗試是否能遇到, 不過這樣效率不高而且一般很難復(fù)現(xiàn)到用戶的crash
Bugly的出現(xiàn)解決的這個問題
Bugly SDK在當(dāng)程序崩潰時, 會自動將錯誤信息發(fā)送到服務(wù)器方便開發(fā)人員查看分析
那么如何使用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; }
這樣當(dāng)程序發(fā)生崩潰時, 崩潰信息會自動發(fā)送到服務(wù)器登錄你的bugly賬號就能查看到了
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法
UIDevice最常見的用法就是用來監(jiān)測iOS設(shè)備的電量了,然后再實現(xiàn)電池狀態(tài)通知非常方便,除此之外還有傳感器等信息的獲取,這里我們就來總結(jié)一下iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法:2016-07-07iOS使用UICollectionView實現(xiàn)列表頭部拉伸效果
這篇文章主要介紹了iOS使用UICollectionView實現(xiàn)列表頭部拉伸效果,OC和Swift兩個版本,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05