ios记录日志和崩溃日志,并将日志文件发送到指定的邮箱

1,AppDelegate中记录日志和异常信息

 

#pragma mark --------test method(save LogInfo to File)--------
- (void)redirectNSlogToDocumentFolder
{
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString *fileName = [NSString stringWithFormat:@"LogInfo.log"];
    NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];
    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+", stdout);
    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+", stderr);
}

void UncaughtExceptionHandler(NSException *exception) {
    NSArray *arr = [exception callStackSymbols];//得到当前调用栈信息
    NSString *reason = [exception reason];//非常重要,就是崩溃的原因
    NSString *name = [exception name];//异常类型
    DEBUGLOG(@"********************************** start record crash log **************************************");
    DEBUGLOG(@"exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, arr);
    DEBUGLOG(@"********************************** end record crash log **************************************");
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //These code will cause that logInfo is saved to File
    
    UIDevice *device = [UIDevice currentDevice];
    if (![[device model]isEqualToString:@"iPad Simulator"]) {
        NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);//记录异常
        [self redirectNSlogToDocumentFolder];//将NSLog信息重定向到文件
    }
    //后面的代码省略
 }

2,发送邮件:

 

            Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
            if (mailClass != nil){
                // We must always check whether the current device is configured for sending emails
                if([mailClass canSendMail]){
                    
                    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
                    NSString *documentDirectory = [paths objectAtIndex:0];
                    NSString *fileName = [NSString stringWithFormat:@"LogInfo.log"];//注意不是NSData!
                    NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];
                    NSURL  *fileUrl = [NSURL fileURLWithPath:logFilePath];
                    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
                    picker.mailComposeDelegate = self;//自己实现代理
                    [picker setToRecipients:@[EmailHost]];//邮箱地址
                    [picker setSubject:NSLocalizedString(@"EmailSubject", nil)];//邮件主题
                    [picker addAttachmentData:[NSData dataWithContentsOfURL:fileUrl] mimeType:@"" fileName:@"LogInfo.log"];
                    
                    NSString *content=NSLocalizedString(@"EmailContent", nil);//邮件正文
                    [picker setMessageBody:content isHTML:NO];
                    [self presentViewController:picker animated:YES completion:^{
                        //DEBUGLOG(@"------");
                    }];
                }else{
                   //SHOW_ALERT(NSLocalizedString(@"EmialNoAccount", @"Your device has not been configured email account."));
                }
            }else{
                //SHOW_ALERT(NSLocalizedString(@"EmialUnSupport", @"Your device does not support email."));
            }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值