In the iOS program crashes, generally we are using Bugtags, Bugly collapse and their Allies, the third party collection, actually the official NSUncaughtExceptionHandler to collect crash information. To implement this method, create a custom UncaughtExceptionHandler class in. H:

@interface CustomUncaughtExceptionHandler : NSObject

+ (void)setDefaultHandler;
+ (NSUncaughtExceptionHandler *)getHandler;

@end
Copy the code

Implement in.m:

# import "CustomUncaughtExceptionHandler. H" / / the address of the sandbox nsstrings * applicationDocumentsDirectory () {return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; } void UncaughtExceptionHandler(NSException * exception) {NSArray * arr = [exception callStackSymbols]; NSString * reason = [exception reason]; // // crash causes can have crash causes (array out of bounds, dictionary nil, call unknown methods...) Crashed controller and method NSString * name = [exception name]; NSString * URL = [NSString stringWithFormat:@" Crash report \ nName :% @nReason :\n% @nCallStackSymbols :\n%@",name,reason,[arr componentsJoinedByString:@"\n"]]; NSString * path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"crash.txt"]; / / to write a TXT file into the sand box [url writeToFile: path atomically: YES encoding: NSUTF8StringEncoding error: nil]; } @implementation CustomUncaughtExceptionHandler + (void)setDefaultHandler { NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler); } + (NSUncaughtExceptionHandler *)getHandler { return NSGetUncaughtExceptionHandler(); } @endCopy the code

Now we have a custom UncaughtExceptionHandler class, and we just need to get the crash file in the appropriate place and upload it to the server, as shown below:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. / / crash log [CustomUncaughtExceptionHandler setDefaultHandler]; / / get crash log, then sending nsstrings * path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory NSUserDomainMask, YES) lastObject]; NSString *dataPath = [path stringByAppendingPathComponent:@"crash.txt"]; NSData *data = [NSData dataWithContentsOfFile:dataPath]; if (data ! = nil) {// send crash log NSLog(@"crash :%@",data); }}Copy the code