cbr*_*hli 31 iphone objective-c uiwebview nsuserdefaults ios
在开始之前,我应该告诉你,这只发生在iOS 5.1中.在最近的更新之前,这从未发生过,并且它仍然不会发生在任何其他版本上.那就是说,这是正在发生的事情.
当用户退出我的应用程序时,发生的一件事就是NSUserDefaults删除了所有内容.而不是手动删除我可能添加到用户默认值的每个键,我只是NSUserDefaults使用此SO问题中建议的方法完全删除所有键:
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
Run Code Online (Sandbox Code Playgroud)
然而,似乎发生了什么,每次我尝试创建一个UIWebView删除后NSUserDefaults,我得到一个EXC_CRASH (SIGABRT).当我打电话时发生了崩溃[[UIWebView alloc] initWithFrame:frame].奇怪吧?完全退出并重新打开应用程序可以UIWebView再次创建s.
所以,我设法找出删除默认值会导致UIWebView问题,但可以肯定的是,我添加了一个符号断点-[NSUserDefaults setObject:forKey:].
![- [NSUserDefaults setObject:forKey:]断点](https://i.stack.imgur.com/0ODbq.png)
创建UIWebView确实会触发断点.
浏览崩溃日志给出了例外原因:
-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: WebKitLocalStorageDatabasePathPreferenceKey)
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪的开始:
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
Run Code Online (Sandbox Code Playgroud)
我现在正在做的,有效的是,只需跟踪NSUserDefaults我设置的密钥,并在需要时手动删除它们.但总有一种风险,我可能会忘记一个敏感的钥匙,所以简单地清除所有这些都会NSUserDefaults让我更加明智.所以,我想知道为什么我不能这样做.这是一个错误还是我做错了什么?
如果您想了解更多信息,请告诉我们!谢谢.
编辑:[[NSUserDefaults standardUserDefaults] synchronize]删除所有后NSUserDefaults做的没有帮助.
ppl*_*ppl 25
我也看到了这个问题,我认为你必须首先创建一个UIWebView,然后才能清除用户默认值.带有以下内容的干净项目将导致iOS5.1崩溃,这在iOS5.0及更早版本中运行良好:
UIWebView *webView = [[UIWebView alloc] init];
[webView release];
[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
UIWebView *anotherWebView = [[UIWebView alloc] init];
[anotherWebView release];
Run Code Online (Sandbox Code Playgroud)
我可以通过这样做来解决崩溃,这可以避免记住所有设置键:
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]];
Run Code Online (Sandbox Code Playgroud)
任何人都看到这样做的任何问题?
| 归档时间: |
|
| 查看次数: |
4848 次 |
| 最近记录: |