Created
February 2, 2015 00:17
-
-
Save CoryFoy/3f77bcada9d6b9824ae2 to your computer and use it in GitHub Desktop.
Catching auth response
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
@implementation ViewController | |
// NSURLConnection Delegates | |
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { | |
if ([challenge previousFailureCount] == 0) { | |
NSLog(@"received authentication challenge"); | |
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"USER" | |
password:@"PASSWORD" | |
persistence:NSURLCredentialPersistenceForSession]; | |
NSLog(@"credential created"); | |
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; | |
NSLog(@"responded to authentication challenge"); | |
} | |
else { | |
NSLog(@"previous authentication failure"); | |
} | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { | |
NSLog(@"didReceiveResponse"); | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { | |
NSLog(@"didReceiveData"); | |
} | |
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { | |
NSLog(@"connectionDidFinishLoading"); | |
} | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
NSString *username = @"hello"; | |
NSString *password = @"world"; | |
NSString *authenticationString = [NSString stringWithFormat:@"%@:%@", username, password]; | |
NSData *authenticationData = [authenticationString dataUsingEncoding:NSASCIIStringEncoding]; | |
NSString *authenticationValue = [authenticationData base64Encoding]; | |
// Setup NSURLConnection | |
NSURL *URL = [NSURL URLWithString:@"http://localhost:3000"]; | |
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL]; | |
[request setValue:[NSString stringWithFormat:@"Basic %@", authenticationValue] forHTTPHeaderField:@"Authorization"]; | |
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; | |
[connection start]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment