我需要将 UIWebView 中的当前位置存储到磁盘并在稍后启动时从磁盘重新加载它。
我之前所做的是将window.scrollX
和window.scrollY
存储在javascript 位置。
这适用于许多情况。
此外,我通过获取电流来存储比例:
webView.scrollView.zoomScale
并通过使用 javascript 将视口(viewport)初始比例设置为它来恢复它。
所有这些都有效,但并不可靠。有没有更好的方法?
我阅读了关于 UIWebView
的 restorationIdentifier
但我不确定是否可以将它保存到磁盘?
最佳答案
In iOS 6 and later, if you assign a value to this view’s restorationIdentifier property, it attempts to preserve its URL history, the scaling and scrolling positions for each page, and information about which page is currently being viewed. During restoration, the view restores these values so that the web content appears just as it did before.
更多来自 Apple Programming Guide和一个 tutorial以及示例项目。您可以运行它并将应用程序发送到后台,然后停止使用 Xcode 运行它,下次打开应用程序时,它会将其状态恢复到应用程序进入后台之前的状态。示例项目可以有一个小修复,在 viewDidAppear
中,如果 self.restoringState
为 NO
,则调用 showPage
功能。
关于javascript - 在 UIWebView 中恢复位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26920561/