iOS NSException崩溃信息统计

该博客聚焦于iOS NSException崩溃统计,在信息技术领域,对iOS系统中NSException引发的崩溃情况进行统计分析,有助于开发者定位问题、优化应用性能,保障应用的稳定性和流畅性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MacUncaughtExceptionHandler : NSObject

+ (void)setDefaultHandler;

+ (NSUncaughtExceptionHandler *)getHandler;

+ (void)TakeException:(NSException *) exception;

@end

NS_ASSUME_NONNULL_END
#import "MacUncaughtExceptionHandler.h"

// 返回沙盒地址
NSString * applicationDocumentsDirectory()
{
    return [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];
}

// 出现崩溃时的回调函数
void UncaughtExceptionHandler(NSException * exception)
{
    NSArray * arr = [exception callStackSymbols];
    // 崩溃的原因  可以有崩溃的原因(数组越界,字典nil,调用未知方法...) 崩溃的控制器以及方法
    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:@"ExceptionLog.txt"];
    // 将txt文件写入沙盒
    [url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
}

@implementation MacUncaughtExceptionHandler

+ (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:@"ExceptionLog.txt"];
    [url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

@end
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface StatisRequestErrorData : NSObject

+ (void)showXcodeInfo;

@end

NS_ASSUME_NONNULL_END
#import "StatisRequestErrorData.h"
#import "MacUncaughtExceptionHandler.h"

@implementation StatisRequestErrorData

#pragma mark -- 崩溃日志
+ (void)showXcodeInfo{
    
    [MacUncaughtExceptionHandler setDefaultHandler];
    
    // 发送崩溃日志
    NSString *path = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];
    NSString *dataPath = [path stringByAppendingPathComponent:@"ExceptionLog.txt"];
    NSData *data = [NSData dataWithContentsOfFile:dataPath];
    if (data != nil) {
        [StatisRequestErrorData sendExceptionLogWithData:data];
    }else{
        NSLog(@"没有崩溃日志");
    }
}

#pragma mark -- 发送崩溃日志
+ (void)sendExceptionLogWithData:(NSData *)data
{
    NSLog(@"======数据上传崩溃日志成功=========");
//    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"ExceptionLog.txt"];
//    [StatisRequestErrorData removeDocumentWithFilePath:path];
}

// 删除本地崩溃日志
+ (BOOL)removeDocumentWithFilePath:(NSString*)filePath{
    
    BOOL isRemove = false;
    NSFileManager* fileManager=[NSFileManager defaultManager];
    if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
        isRemove = [fileManager removeItemAtPath:filePath error:nil];
    }
    return isRemove;
}

@end

使用

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
   //统计崩溃日志
    [StatisRequestErrorData showXcodeInfo];
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄权浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值