为了更好的维护iOSApp,处理程序崩溃是必需要做的,那么如何收集用户使用时出现的崩溃呢?
1.应用崩溃收集方法:腾讯Bugly
腾讯官网 Bugly iOS SDK使用指南:
https://bugly.qq.com/docs/user-guide/instruction-manual-ios/?v=20190108103135
参考:https://www.jianshu.com/p/86ff5f9318ff
2.上传appStore的app,可以通过iTunes Stroe获取,苹果有帮我们收集,
Xcode->Windows->Organizer
3.第三方
通过国内国外平台软件收集日志报告 Crashlytics,Hockeyapp ,友盟 等等。
4.异常捕获
通过iOSSDK中提供了一个现成的函数 NSSetUncaughtExceptionHandler 用来做异常处理
利用NSSetUncaughtExceptionHandler,当程序异常退出的时候,可以先进行处理,然后做一些自定义的动作,并通知开发者,是大多数软件都选择的方法。下面就介绍如何在iOS中实现:
第一步:创建崩溃获取类crash
.h文件
@interfaceCrash :NSObject
//崩溃时的回调函数
voiduncaughtExceptionHandler(NSException *exception);
@end
.m文件:
#import"Crash.h"
@implementation Crash
voiduncaughtExceptionHandler(NSException *exception){
NSArray*stackArry= [exceptioncallStackSymbols];
NSString*reason = [exceptionreason];
NSString*name = [exceptionname];
NSString*exceptionInfo = [NSStringstringWithFormat:@"Exception name:%@\nException reatoin:%@\nException stack :%@",name,reason,stackArry];
NSLog(@"%@",exceptionInfo);
//保存到本地沙盒中
[exceptionInfowriteToFile:[NSStringstringWithFormat:@"%@/Documents/eror.log",NSHomeDirectory()]atomically:YESencoding:NSUTF8StringEncodingerror:nil];
}
@end
第二步在Appdelegate中注册消息方法
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window= [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
self.window.backgroundColor= [UIColorwhiteColor];
//注册消息处理函数的处理方法
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
//发送崩溃日志
NSString*path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];
NSString*dataPath = [pathstringByAppendingPathComponent:@"Exception.txt"];
NSData*data = [NSDatadataWithContentsOfFile:dataPath];
if(data !=nil) {
[self sendExceptionLogWithData:data path:dataPath];
}
}
第三步:崩溃日志发动到服务器
#pragma mark --发送崩溃日志
- (void)sendExceptionLogWithData:(NSData*)data path:(NSString *)path {
AFHTTPSessionManager*manager = [AFHTTPSessionManagermanager];
manager.requestSerializer.timeoutInterval=5.0f;
//告诉AFN,支持接受text/xml的数据
[AFJSONResponseSerializerserializer].acceptableContentTypes= [NSSetsetWithObject:@"text/plain"];
NSString*urlString =@"后台地址";
[managerPOST:urlStringparameters:nilconstructingBodyWithBlock:^(id_NonnullformData) {
[formDataappendPartWithFileData:dataname:@"file"fileName:@"Exception.txt"mimeType:@"txt"];
}success:^(NSURLSessionDataTask*_Nonnulltask,id_Nonnull responseObject) {
//删除文件
NSFileManager*fileManger = [NSFileManagerdefaultManager];
[fileMangerremoveItemAtPath:patherror:nil];
}failure:^(NSURLSessionDataTask*_Nonnulltask,NSError*_Nonnull error) {
}];
}
========================
#import
@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];
}
// 崩溃时的回调函数
void UncaughtExceptionHandler(NSException * exception) {
NSArray * arr = [exception callStackSymbols];
NSString * reason = [exceptionreason]; // // 崩溃的原因 可以有崩溃的原因(数组越界,字典nil,调用未知方法...) 崩溃的控制器以及方法
NSString * name = [exception name];
NSString * url = [NSStringstringWithFormat:@"========异常错误报告========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[arrcomponentsJoinedByString:@"\n"]];
NSString * path = [applicationDocumentsDirectory()stringByAppendingPathComponent:@"Exception.txt"];
// 将一个txt文件写入沙盒
[url writeToFile:pathatomically:YESencoding:NSUTF8StringEncodingerror:nil];
}
@implementation MyUncaughtExceptionHandler
// 沙盒地址
- (NSString *)applicationDocumentsDirectory {
return[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];
}
+ (void)setDefaultHandler {
NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
}
+ (NSUncaughtExceptionHandler *)getHandler {
returnNSGetUncaughtExceptionHandler();
}
+ (void)TakeException:(NSException *)exception {
NSArray * arr = [exception callStackSymbols];
NSString * reason = [exception reason];
NSString * name = [exception name];
NSString * url = [NSStringstringWithFormat:@"========异常错误报告========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[arrcomponentsJoinedByString:@"\n"]];
NSString * path = [applicationDocumentsDirectory()stringByAppendingPathComponent:@"Exception.txt"];
[url writeToFile:pathatomically:YESencoding:NSUTF8StringEncodingerror:nil];
}
//============
//在appledelegate导入头文件加上一个异常捕获监听,用来处理程序崩溃时的回调动作 在这里也要判断一下之前有没有崩溃日志 如果有发送给服务器
//
//- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//#pragma mark -- 崩溃日志
// [MyUncaughtExceptionHandler setDefaultHandler];
// // 发送崩溃日志
// 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 -- 发送崩溃日志
//- (void)sendExceptionLogWithData:(NSData *)data path:(NSString *)path {
//
// AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// manager.requestSerializer.timeoutInterval = 5.0f;
// //告诉AFN,支持接受 text/xml 的数据
// [AFJSONResponseSerializer serializer].acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
// NSString *urlString = @"后台地址";
//
// [manager POST:urlString parameters:nil constructingBodyWithBlock:^(id
_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) {
//
// }];
//}
@end
参考:https://blog.csdn.net/u011146511/article/details/79112508