美文网首页IOS整理
iOS Crash崩溃日志收集

iOS Crash崩溃日志收集

作者: 在这蓝色天空下 | 来源:发表于2016-10-25 19:13 被阅读217次

    1、创建一个SendCrash类
    .h

    //
    //  SendCrash.h
    //  Crash
    //
    //  Created by LC on 16/10/15.
    //  Copyright © 2016年 LC. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface SendCrash : NSObject
    //路径
    + (NSString *)getFilePath;
    //崩溃回调
    + (void)setDefaultHandler;
    
    + (NSUncaughtExceptionHandler *)getHandler;
    //写入
    + (void)TakeException:(NSException *) exception;
    //删除
    + (void)deleteFile;
    //读取
    + (NSString *)readFile;
    @end
    

    .m

    //
    //  SendCrash.m
    //  Crash
    //
    //  Created by LC on 16/10/15.
    //  Copyright © 2016年 LC. All rights reserved.
    //
    
    #import "SendCrash.h"
    
    // 返回沙盒地址
    NSString * applicationDocumentsDirectory()
    {
        return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    }
    
    // 出现崩溃时的回调函数
    void UncaughtExceptionHandler(NSException * exception)
    {
        NSArray * arr = [exception callStackSymbols];
        NSString * reason = [exception reason]; // 崩溃的原因  可以有崩溃的原因(数组越界,字典nil,调用未知方法...) 崩溃的控制器以及方法
        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 SendCrash
    
    // 返回沙盒地址
    + (NSString *)getFilePath
    {
        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];
        
        NSLog(@"11111");
    }
    
    // 删除沙盒里的文件
    + (void)deleteFile {
        NSFileManager* fileManager=[NSFileManager defaultManager];
        //文件名
        NSString *uniquePath = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Exception.txt"];
        BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
        if (!blHave) {
            NSLog(@"no  have");
            return ;
        }else {
            NSLog(@" have");
            BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
            if (blDele) {
                NSLog(@"dele success");
            }else {
                NSLog(@"dele fail");
            }
            
        }
    }
    
    + (NSString *)readFile
    {
        // 拼接要写入文件的路径
        NSString *path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Exception.txt"];
        // 从路径中读取字符串
        NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@", string);
        return string;
    }
    @end
    

    2、在AppDelegate.m里面添加如下代码:

    //
    //  AppDelegate.m
    //  Crash
    //
    //  Created by LC on 16/10/15.
    //  Copyright © 2016年 LC. All rights reserved.
    //
    
    #import "AppDelegate.h"
    #import "SendCrash.h"
    #import "ViewController.h"
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    #pragma mark -- 崩溃日志
        [SendCrash setDefaultHandler];
        // 发送崩溃日志
        NSString *path = [SendCrash getFilePath];
        NSString *dataPath = [path stringByAppendingPathComponent:@"Exception.txt"];
        NSData *data = [NSData dataWithContentsOfFile:dataPath];
        if (data != nil) {
            [self sendExceptionLogWithData:data];
        }
        
        return YES;
    }
    
    #pragma mark -- 发送崩溃日志
    - (void)sendExceptionLogWithData:(NSData *)data
    {
        NSURL*url=[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
        
        [[UIApplication sharedApplication] openURL:url];
        
        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<AFMultipartFormData>  _Nonnull formData) {
            [formData appendPartWithFileData:data name:@"file" fileName:@"Exception.txt" mimeType:@"txt"];
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
           
        } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            
        }];
    }
    

    相关文章

      网友评论

        本文标题:iOS Crash崩溃日志收集

        本文链接:https://www.haomeiwen.com/subject/haoruttx.html