iAsh
iAsh

Reputation: 447

Recognise click on link within web page in iOS

I want to load one URL in web view which is in my application. If user clicks on any URL on that web page then I want to open clicked URL in default safari app i.e. out of my application. I know shouldStartLoadWithRequest: but it will get call whenever new URL starts loading even if it is loading an image in my web page.

How can I recognise that user clicked on URL on my web page?

Upvotes: 0

Views: 665

Answers (1)

joern
joern

Reputation: 27620

You can check the navigationType parameter in webView:shouldStartLoadWithRequest:navigationType:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { 
  switch (navigationType) {
    case UIWebViewNavigationTypeLinkClicked:
      // user clicked on link
    break;
    case UIWebViewNavigationTypeOther: 
      // request was caused by an image that's being loaded
    break;
  }
  return YES; 
}

There are more UIWebViewNavigationTypes that you can use to determine what caused the request:

enum {
   UIWebViewNavigationTypeLinkClicked,
   UIWebViewNavigationTypeFormSubmitted,
   UIWebViewNavigationTypeBackForward,
   UIWebViewNavigationTypeReload,
   UIWebViewNavigationTypeFormResubmitted,
   UIWebViewNavigationTypeOther
};
typedef NSUInteger UIWebViewNavigationType;

Upvotes: 2

Related Questions