1

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

**Hi all, I have a FAQ screen in the app and this is webview, this webpage has as "Email Me" link in it. When clicked on this this navigates to Mail Composer. But app still runs in the background. Now click back the minimized app and click the same FAQ link, app crashes. This only happens in iOS 5.1 . Below are the logs which are received :

-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: WebKitLocalStorageDatabasePathPreferenceKey)

I have used the below code in "APPDelegate" file

NSDictionary *settings = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
    NSArray *keys = [settings allKeys];
    for (int i=0; i<[keys count]; i++)
    {
        NSString *key = [keys objectAtIndex:i];

        [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
Community
  • 1
  • 1
NISHAN gp
  • 121
  • 1
  • 1
  • 5

2 Answers2

0

This crash is not because of version change. The crash log clearly says you are trying to insert nil value in Dictionary and that's why it is crashing :

for (int i=0; i<[keys count]; i++)
{
     NSString *key = [keys objectAtIndex:i];
     if (key != nil)
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}

Try the above code and check.

Rushi
  • 4,553
  • 4
  • 33
  • 46
  • 2
    The result of `key = [keys objectAtIndex:i]` cannot be `nil`, because the `NSArray *keys` cannot contain `nil` objects. So this will probably not help. – Martin R Jan 31 '13 at 06:48
0

Try the below code. Remove anything formNSUserDefaults in this way

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];



NSDictionary *userDefaultsDictionary = [userDefaults dictionaryRepresentation];

NSString *strWebDatabaseDirectory = [userDefaultsDictionary objectForKey:@"WebDatabaseDirectory"];

NSString *strWebKitLocalStorageDatabasePathPreferenceKey = [userDefaultsDictionary objectForKey:@"WebKitLocalStorageDatabasePathPreferenceKey"];



NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];

[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];



[userDefaults removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];



if (strWebDatabaseDirectory) {

    [userDefaults setObject:strWebDatabaseDirectory forKey:@"WebDatabaseDirectory"];}

if (strWebKitLocalStorageDatabasePathPreferenceKey) {

    [userDefaults setObject:strWebKitLocalStorageDatabasePathPreferenceKey forKey:@"WebKitLocalStorageDatabasePathPreferenceKey"];}



[userDefaults synchronize];

Hope it may help you.

Exploring
  • 925
  • 6
  • 18