Forked from ahmetardal/UIWebViewHttpStatusCodeHandling_checksession.m
Created
February 19, 2013 08:48
-
-
Save croath/4984128 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma mark - | |
#pragma mark NSURLConnection Data 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