I already tried getting the current URL of my UIWebVi开发者_JS百科ew
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
.
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
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;
}
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"];
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.
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"];
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSURL *currentURL = [[webView request] URL];
NSLog(@"%@",[currentURL description]);
}
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!
As UIWebView is deprecated, for WKWebview to get the current url is very simple.
webView.url
here the code i use :
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[[webView request] URL] absoluteString]]];
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.
This always works . .
NSString* url= [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
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.
IN Swift try this,
func webViewDidFinishLoad(webView: UIWebView){
println(WebView.request?.mainDocumentURL)
}
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;
}
精彩评论