Question

I'm trying to log crashes in my iPhone app by catching all unhandled exceptions and writing the details to the NSUserDefaults. I set it up by adding this to

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

NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
}

and defined the function to handle it like this:

void uncaughtExceptionHandler(NSException *exception) {

NSMutableString *errorString = [NSMutableString new];
[errorString appendFormat:@"Exception Name : %@\n", exception.name];
[errorString appendFormat:@"Exception Reason : %@\n", exception.reason];
[errorString appendString:@"User Info : \n"];

for (NSString *key in [[exception userInfo] allKeys]) {
    [errorString appendFormat:@"Key : %@ Value : %@\n", key, [[exception userInfo] valueForKey: key]];
}

[errorString appendFormat:@"Call Stack : %@\n", [exception callStackSymbols]];

[[NSUserDefaults standardUserDefaults] setObject:errorString forKey:@"ErrorLog"];

NSLog(@"Exception : %@", errorString);

}

Everything works fine as far as the function being called when there's a crash and the NSLogs print out all the exception details, but nothing ever gets added to the NSUserDefaults, and I have no idea why.

Does anyone have any idea why this wouldn't set an object into the NSUserDefaults?

Thank you for any help...

Était-ce utile?

La solution

Try adding this line after you set the object

[[NSUserDefaults standardUserDefaults] setObject:errorString forKey:@"ErrorLog"];
////add this line//////
[[NSUserDefaults standardUserDefaults] synchronize];
///////////////////////

Autres conseils

You should call '[[NSUserDefaults standardUserDefaults] synchronize];'

By the way, how come do you want to use 'NSUserDefaults'?

While developing, you can see log in console. However, if your App has been released once, crash log in user's device is unreachable so it's useless.

What about using logging service like Flurry or Google analytics?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top