可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I already tried getting the current URL of my UIWebView
with: webview.request.URL
.
Unfortunately the NSURL
was empty. Anything wrong here? I am working with Xcode 3.2.2 beta 5.
The code above should be executed in the UIWebView
delegate didStartLoad
.
回答1:
Matt's version is much cleaner. I recommend everyone to use that one instead of this
You could try this:
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];
回答2:
window.location via JS didn't work reliably for me, but this did:
currentURL = currentWebView.request.URL.absoluteString;
Credit:
http://mohrt.blogspot.com/2008/10/getting-url-from-uiwebview.html
回答3:
here's the code I use to grab the url every time you navigate to a different link within the webview:
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
self.url = aWebView.request.mainDocumentURL;
}
回答4:
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.
回答5:
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:
NSString *url = _webView.request.URL.absoluteString;
The correct one is:
NSString *currentURL = [_webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
回答6:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSURL *currentURL = [[webView request] URL];
NSLog(@"%@",[currentURL description]);
}
回答7:
Tried this for google search results on iPhone:
NSString* currentURL = webView.request.URL.absoluteString;
NSString* mainDocumentURL = webView.request.mainDocumentURL.absoluteString;
Both return the same string!
回答8:
here the code i use :
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[[webView request] URL] absoluteString]]];
回答9:
I use the shouldStartLoadWithRequest
event (UIWebViewDelegate) to catch URL updates. request.mainDocumentURL.absoluteString
will get you the main web page's URL (which is normally what you want), while request.URL.absoluteString
will include CSS and JavaScript includes.
回答10:
This always works . .
NSString* url= [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
回答11:
implement delegate method,
- (BOOL)webView:(UIWebView *)webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *URL = request.URL.absoluteString; NSLog(@"%@",URL);
}
URL is the what you exactly needed.
回答12:
As UIWebView is deprecated, for WKWebview to get the current url is very simple.
webView.url
回答13:
IN Swift try this,
func webViewDidFinishLoad(webView: UIWebView){
println(WebView.request?.mainDocumentURL)
}
回答14:
To get current URL of the WKWebView and UIWebview
Here is the code.
if (self.wkWebView) {
NSString *URL = self.wkWebView.title;
}else if(self.uiWebView) {
NSString *URL = self.uiWebView.request.title;
}