在iOS中捕获崩溃和异常

时间:2013-04-22 04:05:51

标签: ios

我想要的是自己捕获所有崩溃和异常并添加其他信息(例如用户)以保存它。 现在,我的想法是在didFinishLaunchingWithOptions中包装try-catch块。在catch块中,我记录异常和附加信息,然后重新抛出它。 这是实现它的正确方法吗? 提前谢谢。

已更新代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
@try {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    self.loginViewController = [[LoginViewController alloc] init];
    self.window.rootViewController = self.loginViewController;
    [self.window makeKeyAndVisible];
}
@catch (NSException *exception) {
    // Save the exception description and additional info here
    ...
    // And re-throw it.
    [exception raise];
}
    return YES;

}

进一步更新: 我自己尝试了上面的代码,它无法捕捉到其他地方发生的异常。 我知道谷歌分析iOS SDK有“sendUncaughtExceptions”属性做类似的事情。 我想如果我自己实现类似的功能可以提供更多的灵活性,因为我们有自己的错误服务器(我会将错误日志上传到我们的服务器)。 任何建议都表示赞赏。

解决方案: 最后,我从这个博客得到了一个解决方案: http://www.cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html

1 个答案:

答案 0 :(得分:0)

您是否查看了TestFlight API?它实现起来非常简单,免费,听起来就像它可以实现你想要的。它具有内置的远程异常处理和远程日志记录(您只需像使用NSLog一样使用TFLog)。您可以记录UDID并跟踪用户的测试版或企业版,只需注意Apple不允许您在应用商店应用中收集UDID,因此您必须为该版本禁用它 - 您仍然会崩溃日志和其他指标,它们只是匿名。

www.testflightapp.com