您的位置:首页 > 移动开发 > IOS开发

Xcode 【发布状态下如何捕获崩溃日志上传到服务器】

2016-12-30 14:47 274 查看
在发布阶段,如果用户的 App 发生了闪退,也就是崩溃了。我们应该如何得知,应该如何有效收集这些崩溃的日志信息。以助于我们的 App 更好的优化。

去 GitHub 下载一个框架:WZYCrash

https://github.com/CoderZYWang/WZYCrash

将 WZYCrash 集成到自己的项目中去。



共有三个类文件:

WZYUncaughtExceptionHandler(捕获崩溃信息),WZYCrashHandler(上传崩溃信息),WZYTools(获取手机型号)

集成完毕后,我们在 AppDelegate.m 中添加如下代码:
//
// AppDelegate.m
// WZYCrashDemo
//
// Created by CoderZYWang on 2016/12/30.
// Copyright © 2016年 wzy. All rights reserved.
//

#import "AppDelegate.h"

#import "WZYCrashHandler.h"
#import "WZYUncaughtExceptionHandler.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

// 捕获崩溃日志
[WZYUncaughtExceptionHandler setDefaultHandler];
// 上传崩溃日志到服务器
[WZYCrashHandler uploadCrashLog];

return YES;
}

@end


然后我们就可以用我们的代码进行测试,可以先自己写一个小错误然后测试一下是否集成成功。
//
// ViewController.m
// WZYCrashDemo
//
// Created by CoderZYWang on 2016/12/30.
// Copyright © 2016年 wzy. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// 测试代码(运行demo时打开)
// 数组越界错误
// NSArray *arr = @[@"123"];
// NSLog(@"%@", arr[3]);

// 再次运行程序,在崩掉之前会有我们的打印信息(str 就是我们收集的错误信息)
/*
2016-12-30 14:14:26.883423 WZYCrashDemo[2693:1047098] str ---
- IDENTIFIER_NUMBER: 6185368C-0054-49C6-8614-F91EA96A5FF3
- OSVERSION: 10.0.2
- PHONE_TYPE: iPhone 6s
- APP_VERSION: 1.0
- name:
NSRangeException
- reason:
*** -[__NSSingleObjectArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 0]
- callStackSymbols:
0 CoreFoundation 0x00000001844181d8 <redacted> + 148
1 libobjc.A.dylib 0x0000000182e5055c objc_exception_throw + 56
2 CoreFoundation 0x0000000184409428 <redacted> + 0
3 WZYCrashDemo 0x00000001000d3178 -[ViewController viewDidLoad] + 188
4 UIKit 0x000000018a2613dc <redacted> + 1056
5 UIKit 0x000000018a260fa4 <redacted> + 28
6 UIKit 0x000000018a267750 <redacted> + 76
7 UIKit 0x000000018a264bf0 <redacted> + 272
8 UIKit 0x000000018a2d7414 <redacted> + 48
9 UIKit 0x00000
*/
}

@end

还有疑问的话请参见 GitHub 上面的 Demo。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息