Issue: WebView after loading to the first page via LoadData or Loadhtml method, after jumping to the other webview, click the Back button of the navigation bar because WebView The CanGoBack for no causes us to return the first Web page we came in as a loadhtml form.
Cause: WebView CanGoBack is determined by the URL, adding a page of the previous page is loaded in the form of a URL, then we are at this time the cangoback is yes, otherwise no
So we're going to fail with a similar approach.
-(void) goback{
if ([Self.webview CanGoBack]) {
Skip to the original page when you jump to another link in the page by loadhtml or LoadData loaded page
[Self.webview GoBack];
}else{
[Self.navigationviewcontroller Popviewcontroller:yes];
}
}
Bad way: By the State to determine which page to load and then control the GoBack method, (I started with this way to try to find that this approach involves the webview of the delegate method and control the value of the state, so give up).
Solution and Analysis: Since the original page was not returned by loadhtml or LoadData, the first page was not loaded by URL, so we were unable to return to the first web page. Then we'll find a way to load the first page in the form of a URL so everything back to the URL loading page form, you can let the Web itself control GoForward and GoBack, so our method is to get the HTML cache to local, Then we create a URL through LocalPath and then access the first Web page through this URL, so our problem is solved perfectly.
/... when we have get the data or HTML text
1. Cache the data or HTML to local file
NSString *html = @ "a HTML text";
NSString *htmllocalpath = @ "Somepath";
[HTML writetofile:htmllocalpath atomically:yes encoding:nsutf8encoding Error:nil];
2.get the HTML via URL from the local path
Nsurl *url = [Nsurl Urlwithlocalpath:htmllocalpath]
nsurlrequestnsurlrequest alloc] initwithurl:url ];
3.load HTML with Request
[self. WebView loadrequest: request];
ios resolves the first page when the control is returned via LoadData or loadhtml.