Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ahmetardal/1153867 to your computer and use it in GitHub Desktop.
Save ahmetardal/1153867 to your computer and use it in GitHub Desktop.
UIWebViewHttpStatusCodeHandling
#pragma mark -
#pragma mark UIWebViewDelegate Methods
- (BOOL) webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if (_sessionChecked) {
[self log:@"session already checked."];
return YES;
}
[self log:@"will check session."];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
if (conn == nil) {
NSLog(@"cannot create connection");
}
return NO;
}
#pragma mark -
#pragma mark NSURLConnection Delegate Methods
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"connection:didReceiveResponse");
[self log:@"received response."];
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
_sessionChecked = YES;
NSString *newUrl = @"";
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
int status = [httpResponse statusCode];
if (status == 403) {
NSLog(@"not authenticated. http status code: %d", status);
[self log:[NSString stringWithFormat:@"not authenticated. http status code: %d", status]];
newUrl = [NSString stringWithFormat:
@"https://www.google.com/accounts/ServiceLogin?continue=%@", self.url];
}
else {
NSLog(@"authenticated. http status code: %d", status);
[self log:[NSString stringWithFormat:@"authenticated. http status code: %d", status]];
newUrl = self.url;
}
// cancel the connection. we got what we want from the response,
// no need to download the response data.
[connection cancel];
// start loading the new request in webView
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:newUrl]];
[self.webView loadRequest:req];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment