iOS開發(fā)中我們會(huì)遇到程序拋出異常退出的情況北救,如果是在調(diào)試的過程中,異常的信息是一目了然,我們可以很快的定位異常的位置并解決問題震嫉。那么當(dāng)應(yīng)用已經(jīng)打包,iPhone設(shè)備通過ipa的包安裝應(yīng)用后牡属,在使用過程發(fā)現(xiàn)crash票堵,那么如何獲取crash日志呢?對(duì)于保密性要求不高的程序來說,也可以選擇各種一條龍Crash統(tǒng)計(jì)產(chǎn)品逮栅,如 Crashlytics悴势,Hockeyapp 窗宇,友盟,Bugly 等等,不過IOS SDK中提供了一個(gè)現(xiàn)成的函數(shù) NSSetUncaughtExceptionHandler 用來做異常處理
利用NSSetUncaughtExceptionHandler特纤,當(dāng)程序異常退出的時(shí)候军俊,可以先進(jìn)行處理,然后做一些自定義的動(dòng)作,并通知開發(fā)者捧存,是大多數(shù)軟件都選擇的方法粪躬。下面就介紹如何在iOS中實(shí)現(xiàn):
首先創(chuàng)建一個(gè)MyUncaughtExceptionHandler類 (名字可以自己起)實(shí)現(xiàn)崩潰時(shí)調(diào)用的函數(shù),下面我粘貼我程序中的完整代碼,你們需要用的時(shí)候可以直接復(fù)制就可以.
#import <Foundation/Foundation.h>
// 崩潰日志
@interface MyUncaughtExceptionHandler : NSObject
+ (void)setDefaultHandler;
+ (NSUncaughtExceptionHandler *)getHandler;
+ (void)TakeException:(NSException *) exception;
end
#import "MyUncaughtExceptionHandler.h"
#import "AFNetworking.h"
// 返回沙盒地址
NSString * applicationDocumentsDirectory()
{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
// 出現(xiàn)崩潰時(shí)的回調(diào)函數(shù)
void UncaughtExceptionHandler(NSException * exception)
{
NSArray * arr = [exception callStackSymbols];
NSString * reason = [exception reason]; // 崩潰的原因 可以有崩潰的原因(數(shù)組越界,字典nil,調(diào)用未知方法...) 崩潰的控制器以及方法
NSString * name = [exception name];
NSString * url = [NSString stringWithFormat:@"========異常錯(cuò)誤報(bào)告========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[arr componentsJoinedByString:@"\n"]];
NSString * path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Exception.txt"];
// 將txt文件寫入沙盒
[url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
@implementation MyUncaughtExceptionHandler
// 返回沙盒地址
-(NSString *)applicationDocumentsDirectory
{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
+ (void)setDefaultHandler
{
NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
}
+ (NSUncaughtExceptionHandler *)getHandler
{
return NSGetUncaughtExceptionHandler();
}
+ (void)TakeException:(NSException *)exception
{
NSArray * arr = [exception callStackSymbols];
NSString * reason = [exception reason];
NSString * name = [exception name];
NSString * url = [NSString stringWithFormat:@"========異常錯(cuò)誤報(bào)告========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[arr componentsJoinedByString:@"\n"]];
NSString * path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Exception.txt"];
[url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
@end
在appledelegate導(dǎo)入頭文件加上一個(gè)異常捕獲監(jiān)聽,用來處理程序崩潰時(shí)的回調(diào)動(dòng)作 在這里也要判斷一下之前有沒有崩潰日志 如果有發(fā)送給服務(wù)器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#pragma mark -- 崩潰日志
[MyUncaughtExceptionHandler setDefaultHandler];
// 發(fā)送崩潰日志
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *dataPath = [path stringByAppendingPathComponent:@"Exception.txt"];
NSData *data = [NSData dataWithContentsOfFile:dataPath];
if (data != nil) {
[self sendExceptionLogWithData:data];
}
return YES;
}
#pragma mark -- 發(fā)送崩潰日志
- (void)sendExceptionLogWithData:(NSData *)data
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer.timeoutInterval = 5.0f;
//告訴AFN矗蕊,支持接受 text/xml 的數(shù)據(jù)
[AFJSONResponseSerializer serializer].acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
NSString *urlString = @"后臺(tái)地址";
[manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:data name:@"file" fileName:@"Exception.txt" mimeType:@"txt"];
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
}];
}
測(cè)試:
可以隨便建一個(gè)控制器 弄一個(gè)數(shù)組越界測(cè)試一下~ 過兩天我把demo傳上去 給個(gè)地址再發(fā)布更新吧~