Why does clearing NSUserDefaults cause EXC_CRASH later when creating a UIWebView? Why does clearing NSUserDefaults cause EXC_CRASH later when creating a UIWebView? ios ios

Why does clearing NSUserDefaults cause EXC_CRASH later when creating a UIWebView?


I'm seeing this issue as well, I think you have to have created a UIWebView first before clearing user defaults for it to occur. A clean project with the following will cause the crash in iOS5.1, this works fine in iOS5.0 and earlier:

UIWebView *webView = [[UIWebView alloc] init];[webView release];[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];UIWebView *anotherWebView = [[UIWebView alloc] init];[anotherWebView release];

I can work around the crash by doing this instead, which avoids having to remember all your settings keys:

id workaround51Crash = [[NSUserDefaults standardUserDefaults] objectForKey:@"WebKitLocalStorageDatabasePathPreferenceKey"];NSDictionary *emptySettings = (workaround51Crash != nil)                ? [NSDictionary dictionaryWithObject:workaround51Crash forKey:@"WebKitLocalStorageDatabasePathPreferenceKey"]                : [NSDictionary dictionary];[[NSUserDefaults standardUserDefaults] setPersistentDomain:emptySettings forName:[[NSBundle mainBundle] bundleIdentifier]];

Anyone see any issues with doing it this way?


It sounds like you're removing some sort of private preference, which is probably a new bug, either with NSUserDefaults (you shouldnt be able to remove it) or UIWebView (it should cope with a missing entry).

Have you tried the method from the other answer (setting a blank dictionary?). Does that give the same results?

How about if you get the dictionary representation of NSUserDefaults, get all the keys, and iterate through those, removing the objects? (let me know if you need a code sample for that).


It works for me

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];NSDictionary *userDefaultsDictionary = [userDefaults dictionaryRepresentation];NSString *strWebDatabaseDirectory = [userDefaultsDictionary objectForKey:@"WebDatabaseDirectory"];NSString *strWebKitLocalStorageDatabasePathPreferenceKey = [userDefaultsDictionary objectForKey:@"WebKitLocalStorageDatabasePathPreferenceKey"];[userDefaults removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];if (strWebDatabaseDirectory) {    [userDefaults setObject:strWebDatabaseDirectory forKey:@"WebDatabaseDirectory"];}if (strWebKitLocalStorageDatabasePathPreferenceKey) {    [userDefaults setObject:strWebKitLocalStorageDatabasePathPreferenceKey forKey:@"WebKitLocalStorageDatabasePathPreferenceKey"];}[userDefaults synchronize];


matomo