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.
source share