ios 获取 UIWebView 的当前 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2491410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get current URL of UIWebView
提问by danielreiser
I already tried getting the current URL of my UIWebViewwith: webview.request.URL.
Unfortunately the NSURLwas empty. Anything wrong here? I am working with Xcode 3.2.2 beta 5.
我已经尝试通过以下方式获取我的当前 URL UIWebView:webview.request.URL。不幸的NSURL是是空的。这里有什么问题吗?我正在使用 Xcode 3.2.2 beta 5。
The code above should be executed in the UIWebViewdelegate didStartLoad.
上面的代码应该在UIWebView委托中执行didStartLoad。
采纳答案by Rengers
Matt's versionis much cleaner. I recommend everyone to use thatone instead of this
马特的版本要干净得多。我建议大家使用那个而不是这个
You could try this:
你可以试试这个:
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];
回答by Matt Andersen
window.location via JS didn't work reliably for me, but this did:
通过 JS 的 window.location 对我来说并不可靠,但这样做了:
currentURL = currentWebView.request.URL.absoluteString;
Credit: http://mohrt.blogspot.com/2008/10/getting-url-from-uiwebview.html
信用:http: //mohrt.blogspot.com/2008/10/getting-url-from-uiwebview.html
回答by Mark Sands
here's the code I use to grab the url every time you navigate to a different link within the webview:
这是我每次导航到 web 视图中的不同链接时用来抓取 url 的代码:
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
self.url = aWebView.request.mainDocumentURL;
}
回答by SarahR
I too found that the approved answer above was not reliable. But with a slight modification, it seems to work every time:
我也发现上面批准的答案不可靠。但是稍加修改,它似乎每次都有效:
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
Note the addition of ".href" to the Javascript as this is hidden at the end of the line of code.
请注意在 Javascript 中添加了“.href”,因为它隐藏在代码行的末尾。
回答by D.D.
This is not correct, and will return a nil:
这是不正确的,将返回一个零:
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];
However, the code below can get a URL, but the url may not be the current URL:
但是,下面的代码可以获取到一个 URL,但是这个 url 可能不是当前的 URL:
NSString *url = _webView.request.URL.absoluteString;
The correct one is:
正确的一种是:
NSString *currentURL = [_webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
回答by sivasankar
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSURL *currentURL = [[webView request] URL];
NSLog(@"%@",[currentURL description]);
}
回答by Alex Stone
Tried this for google search results on iPhone:
在 iPhone 上为谷歌搜索结果尝试了这个:
NSString* currentURL = webView.request.URL.absoluteString;
NSString* mainDocumentURL = webView.request.mainDocumentURL.absoluteString;
Both return the same string!
两者都返回相同的字符串!
回答by Bobj-C
here the code i use :
这里我使用的代码:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[[webView request] URL] absoluteString]]];
回答by Kris Roofe
回答by Shaik Riyaz
This always works . .
这始终工作。.
NSString* url= [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];

