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.
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.
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.
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.
Try this:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ){
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
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...
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With