Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - UIWebview - Get the URL of the link clicked

I'm creating an app which has a text field & go button on top and web view below them. When user enters URL in text field and clicks "Go" button, it will start loading the page in webview. When user clicks on some link, i want to show the URL of the page (being loaded) in the text field. How can I get that URL of the link clicked.

Also some websites are there which will redirect to some other site. So my question is how to show the URL of the page being loaded in the text field?

like image 209
Satyam Avatar asked Jan 13 '11 11:01

Satyam


3 Answers

Implement this in your UIWebViewDelegate class

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    //CAPTURE USER LINK-CLICK.
      NSURL *url = [request URL];
      yourTextBox.text =   [url absoluteString];


      return YES;   
}
like image 114
Mihir Mehta Avatar answered Nov 17 '22 16:11

Mihir Mehta


If you specifically want the url of links clicked by user then find it in this way.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSLog(@"link clicked = %@",request.mainDocumentURL);
    }
    return YES;
}

Also if you want the url of any request, requested from client side either by taping on a link or specifically requested from webView by you, use

NSLog(@"link clicked = %@",self.webView.request.mainDocumentURL);

and if you want the any current url requested from client side either by you or they are requested by the page you opened automatically, use.

NSLog(@"link clicked = %@",self.webView.request.URL);

This is all that I have found after a long search, may be it will help someone.

like image 23
iHulk Avatar answered Nov 17 '22 15:11

iHulk


There's a delegate method for that purpose, implement it like this:

- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSURL* url = [webView.request URL];
    urlTextField.text = [url absoluteString];
}
like image 24
Felix Avatar answered Nov 17 '22 14:11

Felix