获取UIWebView的当前URL

发布于 2024-08-26 01:48:26 字数 219 浏览 9 评论 0原文

我已经尝试使用 webview.request.URL 获取 UIWebView 的当前 URL。 不幸的是,NSURL 是空的。这里有什么问题吗?我正在使用 Xcode 3.2.2 beta 5。

上面的代码应该在 UIWebView 委托 didStartLoad 中执行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

不气馁 2024-09-02 01:48:26

通过 JS 的 window.location 对我来说并不能可靠地工作,但这确实有效:

currentURL = currentWebView.request.URL.absoluteString;

信用:
http://mohrt.blogspot.com/2008/10/getting -url-from-uiwebview.html

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

甜点 2024-09-02 01:48:26

这是我每次导航到 web 视图中的不同链接时用来获取 url 的代码:

- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
  self.url = aWebView.request.mainDocumentURL;
}

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;
}
美胚控场 2024-09-02 01:48:26

Matt 的版本更加简洁。我建议大家使用那个而不是这个

你可以尝试这个:

NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];

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"];
惟欲睡 2024-09-02 01:48:26

我也发现上面批准的答案并不可靠。
但稍微修改一下,它似乎每次都能工作:

NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];

请注意在 Javascript 中添加“.href”,因为它隐藏在代码行的末尾。

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.

雪落纷纷 2024-09-02 01:48:26

这是不正确的,会返回 nil:

NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];

但是,下面的代码可以获取一个 URL,但该 url 可能不是当前的 URL:

NSString *url = _webView.request.URL.absoluteString;

正确的是:

NSString *currentURL = [_webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];

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"];
£噩梦荏苒 2024-09-02 01:48:26
- (void)webViewDidFinishLoad:(UIWebView *)webView{
     NSURL *currentURL = [[webView request] URL];
     NSLog(@"%@",[currentURL description]);

}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
     NSURL *currentURL = [[webView request] URL];
     NSLog(@"%@",[currentURL description]);

}
半边脸i 2024-09-02 01:48:26

在 iPhone 上尝试使用 google 搜索结果:

 NSString* currentURL = webView.request.URL.absoluteString;
 NSString* mainDocumentURL = webView.request.mainDocumentURL.absoluteString;

两者都返回相同的字符串!

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!

金橙橙 2024-09-02 01:48:26

由于 UIWebView 已被弃用,WKWebview 获取当前 url 非常简单。

webView.url

As UIWebView is deprecated, for WKWebview to get the current url is very simple.

webView.url
百合的盛世恋 2024-09-02 01:48:26

这是我使用的代码:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[[webView request] URL] absoluteString]]];

here the code i use :

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[[webView request] URL] absoluteString]]];
ㄟ。诗瑗 2024-09-02 01:48:26

我使用 shouldStartLoadWithRequest 事件 (UIWebViewDelegate) 来捕获 URL 更新。 request.mainDocumentURL.absoluteString 将获取主网页的 URL(通常是您想要的),而 request.URL.absoluteString 将包含 CSS 和 JavaScript。

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.

哭了丶谁疼 2024-09-02 01:48:26

这总是有效的。 。

NSString* url= [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];

This always works . .

NSString* url= [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
太傻旳人生 2024-09-02 01:48:26

实现委托方法,

- (BOOL)webView:(UIWebView *)webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString  *URL = request.URL.absoluteString;    NSLog(@"%@",URL);
}

URL就是你真正需要的。

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.

久伴你 2024-09-02 01:48:26

Swift 中试试这个,

func webViewDidFinishLoad(webView: UIWebView){
    println(WebView.request?.mainDocumentURL)
}

IN Swift try this,

func webViewDidFinishLoad(webView: UIWebView){
    println(WebView.request?.mainDocumentURL)
}
猫弦 2024-09-02 01:48:26

获取 WKWebView 和 UIWebview 的当前 URL

这是代码。

if (self.wkWebView) {
   NSString  *URL = self.wkWebView.title;
}else if(self.uiWebView) {
   NSString *URL = self.uiWebView.request.title;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文