仅在iOS 5.1中崩溃

时间:2013-01-31 06:43:16

标签: iphone ios objective-c ios5.1

  

可能重复:
  Why does clearing NSUserDefaults cause EXC_CRASH later when creating a UIWebView?

**大家好,我在应用程序中有一个FAQ屏幕,这是webview,这个网页上有“Email Me”链接。单击此按钮时,将导航到Mail Composer。但app仍然在后台运行。现在点击返回最小化的应用程序并单击相同的FAQ链接,应用程序崩溃。这只发生在iOS 5.1中。以下是收到的日志:

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

我在“APPDelegate”文件中使用了以下代码

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];

2 个答案:

答案 0 :(得分:0)

此崩溃不是因为版本更改。崩溃日志明确表示您正在尝试在Dictionary中插入nil值,这就是它崩溃的原因:

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

尝试上面的代码并检查。

答案 1 :(得分:0)

尝试以下代码。以这种方式删除NSUserDefaults中的任何内容

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];

希望它可以帮到你。