Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect redirects of url occurs while loading UIWebView on iphone?

Is there any way to detect url redirects occurs while loading a UIWebView in iphone. when I load www.example.com url, it redirects to some other url. I need to get those redirecting urls and where is actually we handles redirection.

like image 800
neerajPK Avatar asked Jul 06 '12 11:07

neerajPK


People also ask

How do URL redirects work?

In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that start with 3 , and a Location header holding the URL to redirect to. When browsers receive a redirect, they immediately load the new URL provided in the Location header.

What does it mean when a site redirects you?

The too many redirects error indicates that your browser is stuck in an infinite redirection loop. That means your browser is trying to visit one URL which points to another URL, which points back to the first URL, so it's stuck.

How do I set up URL redirection?

Add a new URL redirect Click the URL Redirects tab. In the upper right, click Add URL redirect. In the right panel, select the Standard or Flexible redirect type. A standard redirect is used to redirect one URL to another.


3 Answers

Try this:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
        if ( inType == UIWebViewNavigationTypeLinkClicked ){
           [[UIApplication sharedApplication] openURL:[inRequest URL]];
            return NO;
        }
       return YES;
    }
like image 42
Hemant Dixit Avatar answered Nov 03 '22 09:11

Hemant Dixit


Yes you can do this. Implement

– webView:shouldStartLoadWithRequest:navigationType:

This delegate . This method gets called whenever your webview is about to make a request. So now when someone clicks a button or hyperlink on your webpage, you will get a call to this method. After you catch this call, you can choose to do whatever you want with it. Like redirect the link through your own servers, or log a request to your server about user activity or in your case bring up comments page or change nav bar etc.

Maybe from the request you can figure out if the HTTP response code is 3xx redirection then you can do what you want...

like image 154
Srikar Appalaraju Avatar answered Nov 03 '22 09:11

Srikar Appalaraju


I found this thread when having to do the same. I used Srikar's answer, but here is my code.

I'm using Lumberjack logging, but you can replace the below with just NSLog. You can also get specific parts of the URL if you were looking at host, path, or query string to know if there is a failure (which is what I'm about to have to do). If it redirects several times, each request will get called here and if you want you can stop it and do something or just observe.

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest   navigationType:(UIWebViewNavigationType)inType
{
    DDLogVerbose(@"Redirecting to URL: %@", inRequest.URL.absoluteString);
    return YES;
}
like image 27
sfblackl Avatar answered Nov 03 '22 11:11

sfblackl