如何获取重定向的URL [英] How to get Redirected URL
问题描述
我有一个URL。当我尝试在浏览器中打开它时,它会重定向到另一个URL&显示内容。我想要那个内容但是我没有得到那个重定向的URL。所以,我无法显示数据。
我该如何以编程的方式来做这件事?
我拥有的网址: http://www.windpowerengineering.com/?p=11020
我想要这个重定向的网址。 1)指定你的类符合UIWebViewDelegate协议(并确保你的WebView的代理插座连接到你的视图控制器):
@interface YourWebsiteViewController:UIViewController< UIWebViewDelegate>
2)添加以下代理方法:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSURL * url = [webView.request mainDocumentURL];
NSLog(@The Redirected URL is:%@,url);
}
根据您要使用此信息所做的操作,您可能需要用这个方法代替#2(这会让你有机会阻止加载页面):
$ $ $ $ $ $ $ $ $ - - (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL * url = [request mainDocumentURL];
NSLog(@The Redirected URL is:%@,url);
//如果你想加载页面,返回YES,如果你不加载,则返回NO。
return NO;
}
I have a URL.when i try to open it in browser, it will redirect to another URL & display the Content. I want that content But i don't get that redirected URL. So, I can't able to display data.
How can i do that programmatically??
e.g. URL which i have : http://www.windpowerengineering.com/?p=11020
& the redirected URL is: http://www.windpowerengineering.com/design/mechanical/blades/bladeless-turbine-converts-wind-into-fluid-power/
I want this redirected URL. How can i get this?
1) Specify that your class conforms to the UIWebViewDelegate protocol (and make sure your WebView's delegate outlet is connected to your view controller):
@interface YourWebsiteViewController : UIViewController <UIWebViewDelegate>
2) Add the following delegate method:
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSURL *url = [webView.request mainDocumentURL];
NSLog(@"The Redirected URL is: %@", url);
}
Depending on what you're trying to do with this information, you might want to substitute #2 for this method (which will allow you the opportunity to prevent a page from loading):
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request mainDocumentURL];
NSLog(@"The Redirected URL is: %@", url);
// Return YES if you want to load the page, and NO if you don't.
return NO;
}
这篇关于如何获取重定向的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!