iPhone - UIWebview - Get the URL of the link clicked

后端 未结 3 875
借酒劲吻你
借酒劲吻你 2020-11-30 06:01

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 loadin

相关标签:
3条回答
  • 2020-11-30 06:34

    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;   
    }
    
    0 讨论(0)
  • 2020-11-30 06:38

    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];
    }
    
    0 讨论(0)
  • 2020-11-30 06:40

    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.

    0 讨论(0)
提交回复
热议问题