Scrungepipes
Scrungepipes

Reputation: 37581

How to add navigation when loading differing pages in a UIWebView

I'm loading local .html files into a UIWebView, then if the user clicks on links or buttons within the .html file another page will be loaded with may be either local or remote. (I'm loading these directly into the same UIWebView so Safari isn't being invoked).

If a page linked from the first is loaded I would like the navigation bar to appear and a back button to appear so the user can navigate back to the starting page just as if the web pages were views in a native app. What is the best way of achieving this? Is it possible to do it using native constructs rather than placing a back link or putting some JavaScript etc. in the pages themselves?

Thanks

Upvotes: 1

Views: 809

Answers (1)

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73588

Best way is to implement

– webView:shouldStartLoadWithRequest:navigationType:

This delegate . This method gets called whenever your webview is about to make a request. So now when someone clicks a button on your webpage, you will get a call to this method. After you catch this call, you can choose to do whatever you want with it. Like redirect the link through your own servers, or log a request to your server about user activity etc. In your case show the navigation bar with the Back button activated.

Example - here myMethodAction is where you would want to put your navigation bar appearance logic.

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{ 
    if(navigationType == UIWebViewNavigationTypeLinkClicked)
    {
         if(overrideLinksSwitch.on == TRUE)
         {
             [self myMethodAction];
             [myWebView stopLoading];
             return YES;
         }
        else
        {
            return YES;
        }
    }
    return YES;
}

Hope this helps...

Upvotes: 1

Related Questions