1

I am having one web view in which I am loading one HTML page. It is having document where each page is given link (like an index) at the start.

Requirement is : When click on web view contents URL, open that link in default safari application i.e. outside of an app.

My problem : When I click on content link it detects it as a hyperlink and open it in outside safari browser. If I double click on home button -> select my app -> click on same link again then it is showing me content in the app browser i.e. scrolling down to that content. I am using shouldStartLoadWithRequest delegate method but it does not get call in second scenario.

My Question : How can I achieve both. If some URL gets clicked then open it in outside app. In above case it should just scroll down to show page contents.

Sample code below:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    switch (navigationType) {

        case UIWebViewNavigationTypeFormSubmitted:
            // request was caused by an image that's being loaded
            break;
        case UIWebViewNavigationTypeBackForward:
            // request was caused by an image that's being loaded
            break;
        case UIWebViewNavigationTypeReload:
            // request was caused by an image that's being loaded
            break;
        case UIWebViewNavigationTypeFormResubmitted:
            // request was caused by an image that's being loaded
            break;
        case UIWebViewNavigationTypeLinkClicked: {

            // user clicked on link
            [[UIApplication sharedApplication] openURL:request.URL];

            return NO;
        }
        case UIWebViewNavigationTypeOther:
            // request was caused by an image that's being loaded
            break;
    }

    return YES; 
}

Any help ?

Kaushik Movaliya
  • 799
  • 11
  • 27
iAsh
  • 447
  • 6
  • 19
  • Please clear the question. – gran33 Nov 06 '14 at 07:59
  • delegate of UIWebView is set? – Amandir Nov 06 '14 at 08:19
  • +1 to @gran33. Improve your English, please. – kelin Nov 06 '14 at 09:01
  • Path to follow: Launch app -> click on link present in web view -> safari will open the link -> double click on home button -> tap on app -> click on the same link. Clear question: Why UIWebViewNavigationTypeLinkClicked doesn't get call every time when I click on link? – iAsh Nov 06 '14 at 12:18

1 Answers1

1

The problem is you returning YES from shouldStartLoadWithRequest method after redirecting to safari. To achieve the desired behaviour, you should return NO in the first case, and then, when user returns back to the app and clicks the link second time, you should return YES. That because of WebView loading request only once in your case.

kelin
  • 11,323
  • 6
  • 67
  • 104