问题
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 ,
If you want to get webview loaded page url you can use NSURLRequestObject,something like
NSString * url = [request URL];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