iOS SDK中提供了一個現(xiàn)成的函數(shù) NSSetUncaughtExceptionHandler 用來做異常處理 ,利用NSSetUncaughtExceptionHandler,當(dāng)程序異常退出的時候,可以先進行處理,然后做一些自定義的動作,并通知開發(fā)者。
#import <Foundation/Foundation.h>
// 崩潰日志
@interface MyUncaughtExceptionHandler : NSObject
+ (void)setDefaultHandler;
+ (NSUncaughtExceptionHandler *)getHandler;
+ (void)TakeException:(NSException *) exception;
end
#import "AFNetworking.h"
// 沙盒的地址
NSString * applicationDocumentsDirectory() {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
// 崩潰時的回調(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:@"========異常錯誤報告========\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:@"========異常錯誤報告========\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)入頭文件加上一個異常捕獲監(jiān)聽,用來處理程序崩潰時的回調(diào)動作 在這里也要判斷一下之前有沒有崩潰日志 如果有發(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 path:dataPath];
}
return YES;
}
#pragma mark -- 發(fā)送崩潰日志
- (void)sendExceptionLogWithData:(NSData *)data path:(NSString *)path {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer.timeoutInterval = 5.0f;
//告訴AFN,支持接受 text/xml 的數(shù)據(jù)
[AFJSONResponseSerializer serializer].acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
NSString *urlString = @"后臺地址";
[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) {
// 刪除文件
NSFileManager *fileManger = [NSFileManager defaultManager];
[fileManger removeItemAtPath:path error:nil];
} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
}];
}