为什么我无法将WKWebView保存到[NSUserDefaults standardUserDefaults]? [英] Why I can't save WKWebView to [NSUserDefaults standardUserDefaults]?

查看:122
本文介绍了为什么我无法将WKWebView保存到[NSUserDefaults standardUserDefaults]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

[prefs setObject:self.webView forKey:@"webView"];

上面的代码用于保存WKWebView对象,但我收到编译错误:

The code above is made to save a WKWebView object but I'm getting a compiler error:


2014-11-08 14:58:52.561恢复[2431:482391]属性列表无效
格式:200(属性列表不能包含
类型的对象'CFType')2014-11-08 14:58:52.564恢复[2431:482391]尝试将
设置为非属性列表对象>为
关键webView的NSUserDefaults / CFPreferences值2014-11-08
14:58:52.566恢复[2431:482391] *由于
未捕获异常'NSInvalidArgumentException'而终止应用程序,原因:'尝试
插入非属性列表对象>关键web视图
*表首先掷调用堆栈:(0x2c491c1f 0x39c78c8b 0x2c491b65 0x2c4cbe51 0x2c436599 0x2c4358d7 0x2c4cc0d1 0x2c4cb769 0x2c4ce95b
0x2c4ce875 0x2d0eab69 0x66bdf 0x2f981c2b 0x2f981bd1 0x2f96c863
0x2f98163d 0x2f981317 0x2f97abe1 0x2f9513dd 0x2fbc4c29 0x2f94fe39
0x2c458377 0x2c457787 0x2c455ded 0x2c3a4211 0x2c3a4023 0x3379d0a9
0x2f9b01d1 0x66ead 0x3a1f8aaf)libc ++ abi.dylib:以
终止$ NSException类型的未捕获异常

2014-11-08 14:58:52.561 Restoration[2431:482391] Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType') 2014-11-08 14:58:52.564 Restoration[2431:482391] Attempt to set a non-property-list object > as an NSUserDefaults/CFPreferences value for key webView 2014-11-08 14:58:52.566 Restoration[2431:482391] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object > for key webView' * First throw call stack: (0x2c491c1f 0x39c78c8b 0x2c491b65 0x2c4cbe51 0x2c436599 0x2c4358d7 0x2c4cc0d1 0x2c4cb769 0x2c4ce95b 0x2c4ce875 0x2d0eab69 0x66bdf 0x2f981c2b 0x2f981bd1 0x2f96c863 0x2f98163d 0x2f981317 0x2f97abe1 0x2f9513dd 0x2fbc4c29 0x2f94fe39 0x2c458377 0x2c457787 0x2c455ded 0x2c3a4211 0x2c3a4023 0x3379d0a9 0x2f9b01d1 0x66ead 0x3a1f8aaf) libc++abi.dylib: terminating with uncaught exception of type NSException

如何保存WKWebView并将其恢复历史记录?

How can I save the WKWebView and restore it with its history intact?

编辑:

8个月过去了,这个问题没有解决办法......

8 months have passed and there's no solution to this problem...

编辑2:

我正在制作网络浏览器 ,当用户重新启动网络浏览量时,这一点至关重要(标签)要恢复,这样他就可以按后退按钮然后返回。

I'm making a web browser and it is crucial when the user restarts the app the web views (tabs) to restore so he can press the back button and go back.

如果我将以前的URL保存在一个简单的列表中,我就不能将它们放回到应用重新启动时的Web视图。我只能加载第一页。因此,用户无法按下,因为没有要回的页面。如果它像保存URL列表一样简单我就不会开始赏金。因为,Web浏览器是一个非常复杂的应用程序,我已经非常接近webViews的基础知识,我甚至提交了3个错误,其中只有2个已经解决。包括臭名昭着的截图错误

If I save the previous URLs in a simple list I can't put them back into the web view when the app restarts. I can only load the first page. So, the user can't press back because there is no page to back. If it was as simple as saving a list of URLs I wouldn't start a bounty. Since, a web browser is a very complex app I've been very close to the fundamentals of the webViews and I have even submitted 3 bugs from which only 2 have been resolved. Including the infamous screenshot bug.

standardUserDefaults方法适用于UIWebView,但Apple声明所有开发人员都应该在iOS 7发布之后使用WKWebView。我希望在不久的将来弃用UIWebView。

The standardUserDefaults method does work with UIWebView but Apple states that all developers should use the WKWebView following the release of iOS 7. I'm expecting UIWebView to be deprecated in the near future.

推荐答案

这里没有简单的答案。 WKWebView无法存档,也不参与UI状态保存/恢复。您已经发现了这一点。

There is no easy answer here. The WKWebView cannot be archived and it does also not participate in UI state preservation/restoration. You already discovered this.

对于Firefox for iOS,我们采用了不同的方法来解决这些限制。这远非理想,但确实有效。

For Firefox for iOS we took a different route to work around these limitations. This far from ideal but it does work.

当我们恢复附加了会话信息(以前访问的页面)的标签时,我们从本地网页加载一个特殊的html页面服务器(在localhost上运行),修改页面的推送状态,并推送历史堆栈上以前访问过的URL。

When we restore a tab that has session info (previously visited pages) attached, we load a special html page from a local web server (running on localhost) that modifies the push state of the page and pushes previously visited URLs on the history stack.

因为这些URL需要来源相同,我们基本上推动网址如` http:// localhost:1234 / history / item?url = http://original.url.com

Because these URLs need to be of same origin, we basically push urls like `http://localhost:1234/history/item?url=http://original.url.com

在我们的用户界面中,我们将其翻译为显示 original.url。 COM 。当用户返回历史记录加载这些内容时,我们拦截而不是从localhost加载页面,我们加载原始页面。

In our UI we translate this to display original.url.com. When the user goes back in history to load these, we intercept and instead of loading the page from localhost, we load the original page.

这是一个很大的黑客但是那个就是我们现在所拥有的一切。

It is a big hack but that is all we have at this point.

请参阅 https://github.com/mozilla/firefox-ios

这篇关于为什么我无法将WKWebView保存到[NSUserDefaults standardUserDefaults]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆