Skip to content Skip to sidebar Skip to footer

How To Get Redirected Url

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

Solution 1:

1) Specify that your class conforms to the UIWebViewDelegate protocol (and make sure your WebView's delegate outlet is connected to your view controller):

@interfaceYourWebsiteViewController : 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.returnNO;
}

Solution 2:

use javascript

document.referrer

Solution 3:

Did you try this delegate method?

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

It will be called whenever the webview loads a webpage.

You can get the URL from one of this method's parameters, request

NSURL *regURL=[request URL];
NSString *urlString=[regURL absoluteString];

Solution 4:

That URL is part of the http header. This is what happening:

request 1: http://www.windpowerengineering.com/?p=11020

response 1: page has moved go to http://www.windpowerengineering.com/design/mechanical/blades/bladeless-turbine-converts-wind-into-fluid-power/

and then your browser will go:

request 2: http://www.windpowerengineering.com/design/mechanical/blades/bladeless-turbine-converts-wind-into-fluid-power/

response 3: here is the html

If you look at the http headers from response 1 you will be able to extract the url from the Location header. In Objective C this is one way to do it:

NSURLResponse* response = // the response, from somewhereNSDictionary* headers = [(NSHTTPURLResponse *)response allHeaderFields];
NSString* redirection_url = [headers objectForKey:@"Location"];

In the above code you can access all of the response http headers from the headers dictionary.

Post a Comment for "How To Get Redirected Url"