How does iOS 6 UIWebView state recovery work?

According to docs :

In iOS 6 and later, if you assign a value to this restoreIdentifier property, it tries to preserve its URL history, zoom and scroll fields for each page, and information about which page is currently being viewed. During recovery, the view restores these values ​​so that the web content is displayed as before.

I do all this, but nothing happens. And even if I manually save and restore the URL that the user was viewing earlier, the scroll position is not restored. Are the documents incorrect?

+6
source share
2 answers

I tried Matt's answer, and it works well, however, if there are "advanced" pages in the history stack, they will be replaced by the restored request in the web view object.

The best approach is to instead call the reload method on the webview object, this will restore the history in both directions, as well as the scaling offset of the scaling and content.

If you need an example of my approach or some additional features for your own web presentation, take a look at your open version of SVWebViewController here.

+5
source

The documents are correct, but very incomplete. This is what happens. If the web view is involved in restoring the state (I assume that you know what this means - everything should have a restorationIdentifier , etc.), and if the web view had a request (and not an HTML string) when the user leaves the application, the web view will automatically return to life containing the same request as its request property, and with its unsupported Back and Forward lists. Thus, you can use the state recovery mechanism to restore the state of the web view, but you need to do a little extra dance. This dance is so curious and unclear that initially I had the impression that the state of the web view could not really be saved and restored, despite the approval of the documentation that it could do.

There are two secrets here; once you recognize them, you will understand the restoration of the state of the web view:

  • Restored web browsing will not automatically load your request; what up to your code.

  • After the restored web view has loaded its request, the first element in its back list is the same page in the state that the user left (scrolling and scaling).

Knowing this, you can easily develop a web browsing recovery strategy. First of all, you need to find that we are restoring the state, and raise a flag that says:

 -(void)decodeRestorableStateWithCoder:(NSCoder *)coder { [super decodeRestorableStateWithCoder:coder]; self->_didDecode = YES; } 

Now we can detect (perhaps in viewDidAppear: that we are restoring the state and that the viewDidAppear: magically contains the request and loads this request:

 if (self->_didDecode && wv.request) [wv loadRequest:wv.request]; 

Now for the tricky part. After loading the image, we will immediately “return”. This actually results in restoring the user's previous scroll position (and removing the extra entry from the top of the Back stack). Then we lower our flag so that we do not make this extra move at any other time:

 - (void)webViewDidFinishLoad:(UIWebView *)wv { if (self->_didDecode && wv.canGoBack) [wv goBack]; self->_didDecode = NO; } 

UIWebView is now in the state it was in when the user previously left the application. We saved and restored the state using the built-in function of saving and restoring the state of iOS 6.

+11
source

All Articles