我们都希望我们的 App 永远不会崩溃。然而,当崩溃发生时,从这些崩溃中捕获的日志可以帮助你排除 bug,并使你的 App 重新开始运行。
博主记录三种方式可以查看bug反馈日志:
方式一:使用Xode
1.打开《Xcode》。
2.在菜单栏中选择“Window”(窗口)。
3.前往“Organizer”(组织器)>“Crashes”(崩溃)。
4.在下拉菜单中选择“App Store”。
在这里你可以看到上传APPStore上面应用的崩溃信息。
方式二、集成腾讯Bugly、友盟等第三方统计工具
这里不在重点说明,开发者可以去相应官方自行了解。
方式三、根据公司业务,自定义集成上传APP日志
1.日志的收集初始化
NSException
NSException里面包含了name、reason等信息。
@interface NSException : NSObject <NSCopying, NSSecureCoding> {
@private
NSString *name;
NSString *reason;
NSDictionary *userInfo;
id reserved;
}
注册崩溃收集核心代码:
UIDevice *device = [UIDevice currentDevice];
if([[device model] hasSuffix:@"Simulator"]){
return;
}
NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
signal(SIGABRT, SignalHandler);
signal(SIGILL, SignalHandler);
signal(SIGSEGV, SignalHandler);
signal(SIGFPE, SignalHandler);
signal(SIGBUS, SignalHandler);
signal(SIGPIPE, SignalHandler);
UncaughtExceptionHandler统计崩溃信息:
void UncaughtExceptionHandler(NSException *exception) {
NSMutableDictionary *log = [[NSMutableDictionary alloc]init];
[log setObject:[NSDate date] forKey:@"date"];
// exception info
NSArray *arr = [exception callStackSymbols];
NSString *reason = [exception reason];
NSString *name = [exception name];
[log setObject:name forKey:@"name"];
[log setObject:reason forKey:@"reason"];
[log setObject:[arr componentsJoinedByString:@"\n"] forKey:@"log"];
// Device info
[log setObject:[UIDevice currentDevice].model forKey:@"device"];
[log setObject:[UIDevice currentDevice].systemVersion forKey:@"osversion"];
// batteryLevel info
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
[log setObject:[NSNumber numberWithFloat:[UIDevice currentDevice].batteryLevel*100] forKey:@"batteryLevel"];
[[UIDevice currentDevice] setBatteryMonitoringEnabled:NO];
// Network info
CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [networkInfo subscriberCellularProvider];
[log setObject:networkInfo.currentRadioAccessTechnology forKey:@"networkInfo"];
[log setObject:carrier.carrierName forKey:@"carrierName"];
[log setObject:carrier.isoCountryCode forKey:@"isoCountryCode"];
[log setObject:carrier.mobileCountryCode forKey:@"mobileCountryCode"];
NSString *path = logPathUrl();
NSDictionary *logfile = [NSDictionary dictionaryWithDictionary:log];
[logfile writeToFile:path atomically:YES];
}
2.从本地获取崩溃日
-(NSDictionary *)getCashLog
{
return [NSDictionary dictionaryWithContentsOfFile:logPathUrl()];
}
NSString *logPathUrl() {
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"Exception.txt"];
}
3.上传公司服务器
利用AFNetworking上传本地日志到公司服务器