单军华
2018-07-12 3e8437ae559487362fae3525beb79c534c213a51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
//  main.m
//  screendisplay
//
//  Created by 单军华 on 2018/6/29.
//  Copyright © 2018年 单军华. All rights reserved.
//
 
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
 
void UncaughtExceptionHandler(NSException *exception);
 
 
int main(int argc, char * argv[]) {
    @autoreleasepool {
        @try {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        } @catch (NSException *exception) {
            
            UncaughtExceptionHandler(exception);
        } @finally {
        }
    }
}
 
 
// 设置一个C函数,用来接收崩溃信息
void UncaughtExceptionHandler(NSException *exception) {
    // 可以通过exception对象获取一些崩溃信息,我们就是通过这些崩溃信息来进行解析的,例如下面的symbols数组就是我们的崩溃堆栈。
    NSArray *symbols = [exception callStackSymbols];
    NSString *reason = [exception reason];
    NSString *name = [exception name];
    NSString *caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    
    NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithContentsOfFile:[caches stringByAppendingPathComponent:@"zplan_crash.plist"]];
    
    if (!dictM) {
        dictM = [NSMutableDictionary dictionary];
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyyMMddHHmmssSSS";
    NSString *timestamp = [formatter stringFromDate:[NSDate date]];
    
    
    NSDictionary *dict = @{
                           timestamp : @{
                                   [timestamp stringByAppendingString:@"name"] : name,
                                   [timestamp stringByAppendingString:@"_reason"] : reason,
                                   [timestamp stringByAppendingString:@"__symbols"] : symbols
                                   }
                           };
    [dictM setValuesForKeysWithDictionary:dict];
    [dictM writeToFile:[caches stringByAppendingPathComponent:@"zplan_crash.plist"] atomically:YES];
    
}