1
 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.

Suyash Seth
  • 85
  • 1
  • 10

4 Answers4

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"];

Community
  • 1
  • 1
Abhinandan Sahgal
  • 1,076
  • 1
  • 13
  • 27
  • I want the second one but i print url.the url gives ''. – Suyash Seth Dec 02 '14 at 11:32
  • How have you used this , inside a tap event ? Just to assure that webview interaction is enabled . – Abhinandan Sahgal Dec 02 '14 at 11:56
  • i am creating uiwebview dynamically ad set its delegates. – Suyash Seth Dec 02 '14 at 11:58
  • You can try this code inside this method - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType; – Abhinandan Sahgal Dec 02 '14 at 12:00
  • NSString *url = [webView stringByEvaluatingJavaScriptFromString:@"window.location"]; Put this code inside this - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType; – Abhinandan Sahgal Dec 02 '14 at 12:06
  • it return @"" when i click on hyperlink. – Suyash Seth Dec 02 '14 at 12:10
  • Added new answer ,take a look . – Abhinandan Sahgal Dec 02 '14 at 12:14
  • @AbhinandanSahgal that's not how this works you shouldn't be telling people to upvote your answer whether it helped or not. I purposely avoid upvoting answers where the user has told me to do so. And I just realised the answer I've already commented on below is yours as well so -1 for trying to gain double points. – Popeye Dec 02 '14 at 15:11
1

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;
}
Pandey_Laxman
  • 3,889
  • 2
  • 21
  • 39
1

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

inVINCEable
  • 2,167
  • 3
  • 25
  • 49
  • 1
    According to http://www.raywenderlich.com/76735/using-nsurlprotocol-swift it should be `webView.request.URL.absoluteString` – Popeye Dec 02 '14 at 14:47
  • In all honesty I was questioning myself whether I was wrong. I don't do that much swift but as far as I knew that hadn't changed so I had to do a little search before hand. – Popeye Dec 02 '14 at 14:50
0

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

Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42