How to get url which I hit on UIWebView?

那年仲夏 提交于 2019-12-11 12:28:46

问题


 NSString *testString =[NSString stringWithFormat:@" <a href = \\\\ \"www.google.com\">Google</a>"];
[webView loadHTMLString:testString baseURL:nil];

I want to get the URL when I click on this UIWebView content in

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {

if ( inType == UIWebViewNavigationTypeLinkClicked ) {
    [[UIApplication sharedApplication] openURL:inRequest.mainDocumentURL];
    return NO;
}

return YES;

}

delegate method. When I click google hyperlink in webview it gives

URL: applewebdata://

Please help me.


回答1:


You question is not very clear if you want to get the tapped url on webview or the webview's loaded page url ,

  1. If you want to get webview loaded page url you can use NSURLRequestObject,something like

    NSString * url = [request URL];

  2. If you want to get the clicked link url , you will have to use this java script

    NSString *url = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];




回答2:


you can do like this way

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL.absoluteString rangeOfString:@"www.google.com"].location!=NSNotFound)
    {
        [[UIApplication sharedApplication]openURL:request.URL];
        return NO;
    }
 return YES;
}



回答3:


For Swift:

Whenever your webpage is loaded you can use this, I use it for a label in this case:

webUrlLabel.text = webView.request.URL.absoluteString

For Objective-C:

NSString *currentPage = [NSString stringWithFormat:@"%@", webView.request.URL.absoluteString];

webUrlLabel is a UILabel

webView is my UIWebView




回答4:


You already have NSURLRequest object. Access url from that.

inRequest.URL

way 1:

NSString *currentURL = myWebView.request.URL.absoluteString;

Way 2:

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


  return YES;   

}

Please let me know if it works. Thanks



来源:https://stackoverflow.com/questions/27247873/how-to-get-url-which-i-hit-on-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!