IOS apps capture exceptions and send feedback to servers to improve user experience.
In the app we develop, it is inevitable that sometimes users will crash when using the software. we need to capture exceptions. we can add the corresponding code to the entry class. Each time the user opens the program, we can check whether there is a crash log in the sandbox. If yes, it can be sent to the server to facilitate software improvement.
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
Self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds];
// Override point for customization after application launch.
Self. window. backgroundColor = [UIColor whiteColor];
[Self. window makeKeyAndVisible];
ExceptionHandler macro definition for capturing exceptions
// Feedback to the server
Self. window. rootViewController = [ViewController new];
Return YES;
}
Macro definition
# Define ExceptionHandler [ZYExceptionHandler caughtExceptionHandler];
# Import "ZYExceptionHandler. h"
# Include <libkern/OSAtomic. h>
# Include <execinfo. h>
@ Implementation ZYExceptionHandler
+ (Void) caughtExceptionHandler {
// Specify the crash processing method.
NSSetUncaughtExceptionHandler (& UncaughtExceptionHandler );
}
+ (Void) fileCreate {
NSString * path = [ZYExceptionHandler exceptionPath];
NSFileManager * manager = [NSFileManager defamanager manager];
// Create when the file does not exist
If (! [Manager fileExistsAtPath: path])
{
NSString * dateString = [ZYExceptionHandler currentTime];
NSString * logStr = [NSString stringWithFormat: @ "=========================\ n File Creation Time: % @ \ n ====================== ", dateString];
NSData * data = [logStr dataUsingEncoding: NSUTF8StringEncoding];
[Data writeToFile: path atomically: YES];
}
}
Void UncaughtExceptionHandler (NSException * exception ){
/**
* Obtain the exception and crash information
*/
// Create a crash-accepted file here
[ZYExceptionHandler fileCreate];
NSArray * callStack = [exception callStackSymbols];
NSString * reason = [exception reason];
NSString * name = [exception name];
NSString * dateString = [ZYExceptionHandler currentTime];
NSString * systemName = [[UIDevice currentDevice] systemName];
NSString * strModel = [[UIDevice currentDevice] model];
NSDictionary * infoDict = [[NSBundle mainBundle] infoDictionary];
NSString * bundleIdentifier = infoDict [@ "CFBundleIdentifier"];
NSString * versionNum = [infoDict objectForKey: @ "cfbundlepolicversionstring"];
NSString * content = [NSString stringWithFormat: @ "\ n ========= Exception error report ===========\ n error time: % @ system: % @ device: % @ \ n current version: % @ current unique identifier: % @ \ n error name: % @ \ n error cause: \ n % @ \ ncallStackSymbols: \ n % @ \ n ========= Exception error end =========\ n ", dateString, systemName, strModel, versionNum, bundleIdentifier, name, reason, [callStack componentsJoinedByString: @ "\ n"];
NSString * path = [ZYExceptionHandler exceptionPath];
NSFileHandle * outFile = [NSFileHandle fileHandleForWritingAtPath: path];
// Locate and locate the end position of outFile (append the file here)
[OutFile seekToEndOfFile];
[OutFile writeData: [content dataUsingEncoding: NSUTF8StringEncoding];
// Close the read/write File
[OutFile closeFile];
}
+ (NSString *) exceptionPath {
NSLog (@ "----- >>>%@", NSHomeDirectory ());
NSString * documents = [NSHomeDirectory () stringByAppendingPathComponent: @ "Documents"];
NSString * path = [documents stringByAppendingPathComponent: @ "exceptionHandler.txt"];
Return path;
}
+ (NSString *) currentTime {
NSDate * date = [NSDate date];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[Formatter setDateFormat: @ "yyyy-MM-dd hh: mm"];
NSString * dateString = [formatter stringFromDate: date];
Return dateString;
}
// Obtain the call stack
+ (NSArray *) backtrace
{
Void * callstack [1, 128];
Int frames = backtracing (callstack, 128 );
Char ** strs = backtrace_symbols (callstack, frames );
NSMutableArray * backtrace = [NSMutableArray arrayWithCapacity: frames];
For (int I = 0; I <frames; I ++)
{
[Backtrace addObject: [NSString stringwithuf8string: strs [I];
}
Free (strs );
Return backtrace;
}
@ End