ios resolves the first page when the control is returned via LoadData or loadhtml.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.