Before I begin, I should tell you that this only happens in iOS 5.1. Before the most recent update, this had never happened and it still does not happen on any other version. That said, here's what's going on.
When a user logs out of my app, one of the things that happens is that all of the NSUserDefaults
get deleted. Rather than manually removing every key I might add to the user's defaults, I just completely delete all of the NSUserDefaults
, using the method suggested in this SO question:
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
What seems to happen though, is that every time I try to create a UIWebView
after having removed NSUserDefaults
, I get an EXC_CRASH (SIGABRT)
. The crash happens when I call [[UIWebView alloc] initWithFrame:frame]
. Strange right? Completely quitting and reopening the app allows UIWebView
s to be created again.
So, I managed to figure out that removing the defaults would cause the UIWebView
issue, but to be sure, I added a symbolic breakpoint for -[NSUserDefaults setObject:forKey:]
.
Creating a UIWebView
does indeed trigger the breakpoint.
Poking through the crash logs gives me the exception reason:
-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: WebKitLocalStorageDatabasePathPreferenceKey)
And here's the beginning of the stack trace:
0 CoreFoundation 0x3340688f __exceptionPreprocess + 163
1 libobjc.A.dylib 0x37bd4259 objc_exception_throw + 33
2 CoreFoundation 0x33406789 +[NSException raise:format:] + 1
3 CoreFoundation 0x334067ab +[NSException raise:format:] + 35
4 CoreFoundation 0x3337368b -[__NSCFDictionary setObject:forKey:] + 235
5 WebKit 0x3541e043 -[WebPreferences _setStringValue:forKey:] + 151
6 UIKit 0x32841f8f -[UIWebView _webViewCommonInit:] + 1547
7 UIKit 0x328418d7 -[UIWebView initWithFrame:] + 75
8 MyApp 0x0007576f + 0
9 UIKit 0x326d4dbf -[UIViewController view] + 51
10 UIKit 0x327347e5 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 93
11 UIKit 0x32734783 -[UITabBarController transitionFromViewController:toViewController:] + 31
12 UIKit 0x327340bd -[UITabBarController _setSelectedViewController:] + 301
13 UIKit 0x327bd5d9 -[UITabBarController _tabBarItemClicked:] + 345
What I'm doing for now, and what works, is just keeping track of the NSUserDefaults
keys I have set and removing them all manually when I need to. But there's always a risk I might forget a sensitive key, so simply clearing all NSUserDefaults
strikes me as more sensible. So, I would like to know why I can't do that. Is it a bug or am I doing something wrong?
If you want any more info, just let me know! Thanks.
EDIT: Doing [[NSUserDefaults standardUserDefaults] synchronize]
after deleting all NSUserDefaults
does not help.