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

前端 未结 4 1060
误落风尘
误落风尘 2020-12-15 03:36

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 othe

4条回答
  •  太阳男子
    2020-12-15 04:01

    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?

提交回复
热议问题