[IOS] 類似 LeanCloud 的 Crash 收集實現

來源:互聯網
上載者:User

標籤:

用慣了 crashlytics  ,Crash 收集功能的確很強大,各種 Crash 收集,而且即時給開發人員發送郵件

詳情見:唐巧一篇文章

國內的 Crash 的第三方,相比之下就 Low 很多,畢竟人家也是專業做 Crash 的,貌似被 Twitter收購了

但是你想過沒有自己實現一個簡單的 Crash 收集呢,比國內的第三方簡陋些,可以實現準系統呢?

先說 Crash ,App 最致命的問題,使用者第一反應可以刪,然後再說說對 Crash  怎麼辦?

NSDictionary  Crash ,Range 超過範圍了Crash ,各種 Crash

首先想到的是, Try Catch ,實現如下:

    @try {        // 1        NSString *str = @"hello world";        [str substringFromIndex:200]; // 程式到這裡會崩    }    @catch (NSException *exception) {        // 2        NSLog(@"%s\n%@", __FUNCTION__, exception);        //        @throw exception; // 這裡不能再拋異常    }    @finally {        // 3        NSLog(@"最終,我還是執行啦");    }    // 4    // 這裡一定會執行    NSLog(@"Yeah,pass");

有點意思,調試時方便很多,對資料模型處理時方便很多,但是總有想不到的Crash

同時 也看到一個對 基礎資料型別 (Elementary Data Type)、KVO 的 Crash 處理的 第三方還不錯推薦下 DurexKit

http://code4app.com/ios/DurexKit%E5%AE%89%E5%85%A8%E5%B7%A5%E5%85%B7%E5%8C%85/5325b421933bf0463d8b49ec 


回到正題,國內Crash收集是什麼樣子,比如 Crash 的 log ,給個 AVOS 的 Crash log


 自己可不可以實現呢? 從郵件的資訊看,基本和 AVOS Log  一致 可以實現簡單的Crash 收集

代碼如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    // 最好放在其他代碼之前    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);        NSString *str = @"abc";    [str substringFromIndex:111]; // 程式到這裡會崩        return YES;}

void UncaughtExceptionHandler(NSException *exception) {    /**     *  擷取異常崩潰資訊     */    NSArray *callStack = [exception callStackSymbols];    NSString *reason = [exception reason];    NSString *name = [exception name];    NSString *content = [NSString stringWithFormat:@"========異常錯誤報表========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[callStack componentsJoinedByString:@"\n"]];        /**     *  把異常崩潰資訊發送至開發人員郵件     */    NSMutableString *mailUrl = [NSMutableString string];    [mailUrl appendString:@"mailto:[email protected]"];    [mailUrl appendString:@"?subject=程式異常崩潰,請配合發送異常報告,謝謝合作!"];    [mailUrl appendFormat:@"&body=%@", content];       // 開啟地址    NSString *mailPath = [mailUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailPath]];}

郵件內容如下,基本實現了AVOS的Crash 收集功能,收集裝置型號、系統自行加代碼:





[IOS] 類似 LeanCloud 的 Crash 收集實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.