iOS程序中捕获异常退出信息,并提交服务器

可以通过写一个类CrashExceptioinCatcher,在类中定义一个静态方法startCrashExceptionCatch, 方法里调NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);

绑定void uncaughtExceptionHandler(NSException *exception)方法处理异常信息,在void uncaughtExceptionHandler(NSException *exception)里将异常打印出来,并附带上设备信息提交至服务器,这样在测试时候能够比较有效的收集异常信息。

头文件

#import <Foundation/Foundation.h>  
  
@interface CrashExceptioinCatcher : NSObject  
  
+ (void)startCrashExceptionCatch;  
  
@end  

实现文件

#import "CrashExceptioinCatcher.h"  
  
// 提交异常Log信息  
void uncaughtExceptionHandler(NSException *exception) {  
      
    // 异常Log信息  
    NSString *logInfo = [NSString stringWithFormat:@"Crash:\n%@\nStack Trace:\n%@\n",  
                         [exception description], [exception callStackSymbols]];  
    NSLog(@"%@", logInfo);  
      
    // TODO: 提交服务器收集  
    // ....  
}  
  
  
@implementation CrashExceptioinCatcher  
  
+ (void)startCrashExceptionCatch  
{  
    // Sets the top-level error-handling function where you can perform last-minute logging before the program terminates.  
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); //设置异常Log信息的处理  
}  
  
@end  

使用方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  
{  
      
    [CrashExceptioinCatcher startCrashExceptionCatch];  
      
    // .................  
      
}  

 转自:http://www.open-open.com/lib/view/open1389252119727.html

posted @ 2014-04-14 15:03  天下.无贼  阅读(281)  评论(0)    收藏  举报