I'm creating an app which has a text field & go button on top and web view below them. When user enters URL in text field and clicks "Go" button, it will start loading the page in webview. When user clicks on some link, i want to show the URL of the page (being loaded) in the text field. How can I get that URL of the link clicked.
我正在创建一个应用程序,上面有一个文本字段& go按钮,下面是web view。当用户在文本框中输入URL并单击“Go”按钮时,将开始在webview中加载页面。当用户单击某个链接时,我想在文本字段中显示正在加载的页面的URL。我怎么才能让链接的URL被点击。
Also some websites are there which will redirect to some other site. So my question is how to show the URL of the page being loaded in the text field?
也有一些网站会跳转到其他网站。我的问题是如何显示在文本框中加载的页面的URL ?
3 个解决方案
#1
44
Implement this in your UIWebViewDelegate class
在UIWebViewDelegate类中实现这个
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
NSURL *url = [request URL];
yourTextBox.text = [url absoluteString];
return YES;
}
#2
13
If you specifically want the url of links clicked by user then find it in this way.
如果您特别想要用户单击的链接的url,那么就以这种方式查找它。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSLog(@"link clicked = %@",request.mainDocumentURL);
}
return YES;
}
Also if you want the url of any request, requested from client side either by taping on a link or specifically requested from webView by you, use
另外,如果您想要任何请求的url(客户端通过在链接上粘贴或您从webView中特别请求的请求),请使用
NSLog(@"link clicked = %@",self.webView.request.mainDocumentURL);
and if you want the any current url requested from client side either by you or they are requested by the page you opened automatically, use.
如果您想要客户端请求的任何当前url,或者您自动打开的页面请求它们,请使用。
NSLog(@"link clicked = %@",self.webView.request.URL);
This is all that I have found after a long search, may be it will help someone.
这是我找了很久才找到的,也许会对别人有所帮助。
#3
5
There's a delegate method for that purpose, implement it like this:
有一个委托方法,它是这样实现的:
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSURL* url = [webView.request URL];
urlTextField.text = [url absoluteString];
}
#1
44
Implement this in your UIWebViewDelegate class
在UIWebViewDelegate类中实现这个
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
NSURL *url = [request URL];
yourTextBox.text = [url absoluteString];
return YES;
}
#2
13
If you specifically want the url of links clicked by user then find it in this way.
如果您特别想要用户单击的链接的url,那么就以这种方式查找它。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSLog(@"link clicked = %@",request.mainDocumentURL);
}
return YES;
}
Also if you want the url of any request, requested from client side either by taping on a link or specifically requested from webView by you, use
另外,如果您想要任何请求的url(客户端通过在链接上粘贴或您从webView中特别请求的请求),请使用
NSLog(@"link clicked = %@",self.webView.request.mainDocumentURL);
and if you want the any current url requested from client side either by you or they are requested by the page you opened automatically, use.
如果您想要客户端请求的任何当前url,或者您自动打开的页面请求它们,请使用。
NSLog(@"link clicked = %@",self.webView.request.URL);
This is all that I have found after a long search, may be it will help someone.
这是我找了很久才找到的,也许会对别人有所帮助。
#3
5
There's a delegate method for that purpose, implement it like this:
有一个委托方法,它是这样实现的:
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSURL* url = [webView.request URL];
urlTextField.text = [url absoluteString];
}