Restore position in UIWebView

I need to save the current position in the UIWebView to disk and reload it from disk on next startup.

What I did before was to save window.scrollX

and window.scrollY

use javascript. This is good for many situations.

Also, I keep the scale by getting the current one:

webView.scrollView.zoomScale

      

and restore it by setting the initial viewport scale for it using javascript.

All of this works, but is not reliable. Is there a better way?

I read about UIWebView

restorationIdentifier

, but I'm not sure if I can save it to disk?

+3


source to share


2 answers


In iOS 6 and later, if you assign a value to these views with the restoreIdentifier property, it tries to store its history, zoom and scroll url for each page and information about which page is currently being viewed. During the restoration, the view restores these values ​​so that the web content appears the same as before.



See the Apple Programming Guide and tutorial along with the sample project for details . You can start it and send the app in the background and then stop it from running with Xcode, the next time you open the app it will restore its state to the state before the app goes into the background. There might be a small fix in the sample project, in viewDidAppear

, if self.restoringState

- NO

, you call the function showPage

.

+3


source


To be clear, the steps for restoring the state of the UIWebView (similar to normal restoring the state) are as follows:

  • In the application's deletion application: shouldRestoreApplicationState

    , it application: shouldSaveApplicationState

    returns true.
  • The presented view controller - set its recovery id - you can do this in the storyboard identity inspector. The identifier can be any unique string. If you have a VC container (navigation, etc.) you need to set a restore ID for it.
  • Set recovery ID for UIWebView in your storyboard.
  • Replace applicationFinishedRestoringState

    and rebootwebView

I used this step and everything seemed to work. However, after reading your comments, I saw that you have problems with enlarged pages. The problem is an error in restoring the UIWebView (I think) and it has to do with the reload mechanism. That's why I suggest the following hack:

import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet var webView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        webView.delegate = self
        webView.scrollView.restorationIdentifier = "webViewScrollView"
    }

    var restoring = false
    override func viewDidAppear(animated: Bool) {
        if !restoring {
            webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://google.com")!))
        }

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    var restoredScrollOffset: CGPoint = CGPoint.zeroPoint
    override func applicationFinishedRestoringState() {
        restoring = true
        restoredScrollOffset = webView.scrollView.contentOffset
        webView.reload()
    }

    func webViewDidFinishLoad(webView: UIWebView) {
        if restoring {
            restoring = false
            if webView.scrollView.contentOffset != restoredScrollOffset {
                println("bug - sometimes it may happen")
            }
            webView.scrollView.contentOffset = restoredScrollOffset
        }
   }

      



because this is a hack, we cannot be sure it will be flawless :)

Please note that I am setting the scrollview restore id in web mode, so I will not need to implement the VC encoding feature - it is possible to encode only the shift of the scrollview using the VC encode / decode override.

I only set the offset of the scroll view; if you're having trouble with the zoom scale (which I didn't find on any webpage I've tried) you can just set the zoom to the same location where I set the offset of the scroll content.

The code is at https://github.com/iamdoron/UIWebViewRestoration

+1


source







All Articles