获取已发布的app错误信息:

获取已发布的app错误信息:

第一种方法:

在uiapplication.m添加

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

NSSetUncaughtExceptionHandler(&myExceptionHandler);

添加以上代码

//处理异常===========

void myExceptionHandler(NSException *exception)

{

    NSArray *stack = [exception callStackReturnAddresses];

    NSLog(@"Stack trace: %@", stack);

    

    NSArray *arr = [exception callStackSymbols];

    NSString *reason = [exception reason];

    NSString *name = [exception name];

    

    

    //调用发邮件处理错误

/*    NSString *urlStr = [NSString stringWithFormat:@"mailto://[email protected]?subject=bug报告&body=感谢您的配合!<br><br><br>" "错误详情:<br>%@<br>---------<br>%@<br>------<br>%@",name,reason,[arr componentsJoinedByString:@"<br>"]];

    

    NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    

    [[UIApplication sharedApplication] openURL:url];  */

    

    

    //保存在txt文档中,然后在发邮件出去,文档要加密,才能添加附件成功。

    NSString *txtUrl = [NSString stringWithFormat:@"========异常崩溃报=====\nname:\n%@\nreason: \n%@\ncallStackSymbols:\n%@",name,reason,[arr componentsJoinedByString:@"\n"]];

    

    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Exception.txt"];

    

    [txtUrl writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

    

    NSString *urlStr = [NSString stringWithFormat:@"mailto://[email protected]?subject=bug报告&attachment%@&body=感谢您的配合!<br><br><br>" "错误详情:<br>%@<br>---------<br>%@<br>------<br>%@",path,name,reason,[arr componentsJoinedByString:@"<br>"]];

    

     NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    [[UIApplication sharedApplication] openURL:url];


}



第二种方法:

如果不是在模拟器上,又或者我们的设备没有连接到PC上,那么如何调试我们的程序呢?假如我们通过AdHoc发布了程序,希望随时得到测试人员的反馈,可以利用下面的方法,将标准出力(stderr)信息记录到文件中,然后通过邮件新式发给开发者。

1. 设置一个开关,用来清空日志文件内容,并切换输出位置;

1

2

3

4

5

6

7

8

- (BOOL)deleteLogFile {


    [self finishLog];

    BOOL success = [[NSFileManager defaultManager] removeItemAtPath:[self loggingPath] error:nil];

    [self startLog];

    return success;


}


当我们调用上面的deleteLogFile后,就会清空之前的日志,并设置新的输出。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

- (void)finishLog {


    fflush(stderr);

    dup2(dup(STDERR_FILENO), STDERR_FILENO);

    close(dup(STDERR_FILENO));


}


- (NSString*)loggingPath {


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];

    return logPath;


}


- (void)startLog {

   freopen([[self loggingPath] cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);

}


2. 当日志取得之后,可以通过下面的方式发送邮件给开发者

1

2

3

4

5

6

7

8

9

10

11

- (void)sendLogByMail {


    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

    picker.mailComposeDelegate = self;

    [picker setSubject:[NSString stringWithFormat:@"%@ - Log", [self appName]]];

    NSString *message = [NSString stringWithContentsOfFile:[self loggingPath] encoding:NSUTF8StringEncoding error:nil];

    [picker setMessageBody:message isHTML:NO];

    [self.navigationController presentModalViewController:picker animated:YES];

    [picker release];


}


猜你喜欢

转载自blog.csdn.net/kfq0071/article/details/40372679