Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm new to objective c and what i'm trying to do is request the source code of a webpage that uses network credentials authentication. I used to do it in C# using httpwebrequest and network credentials but any ideas how to do this in objective C.
Posted
Updated 8-Mar-12 5:16am
v3

1 solution

For authentication u need to use NSUrlConnection and its delegate methods.

Delegate methods:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[connection cancel]; // connection is an NSUrlConnection object
connection=nil;

// Do ur code here
}


-(BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
return NO;
}


-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace*)protectionSpace
{
if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic] ||
[protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest])
{
//NSLog(@"NSURLAuthenticationMethodHTTPBasic");
if (nil != usernameTextField.text)
return YES;

[self showAlertView];
[connection cancel];
connection = nil;
return NO;
}
else
{
//NSLog(@"NSURLAuthenticationMethodClientCertificate or NSURLAuthenticationMethodServerTrust");
return NO;
}
return YES;
}


-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] == 0)
{
NSURLCredential *newCredential;

newCredential = [NSURLCredential credentialWithUser:usernameTextField.text

password:passwordTextField.text

persistence:NSURLCredentialPersistencePermanent];

[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];

[usernameTextField release], usernameTextField = nil;
[passwordTextField release], passwordTextField = nil;
}
else
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
// inform the user that the user name and password
// in the preferences are incorrect
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Preferences Credentials Are Incorrect" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
}
}


-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot Open Page"
message:@"Server is not responding"
delegate:nil cancelButtonTitle:@"Ok"
otherButtonTitles:nil];

[alert show];
[alert release];
}


-(void)showAlertView
{
NSString *messageString = connectionString;
if (messageString.length > 32)
messageString = [messageString substringToIndex:32];
messageString = [messageString stringByAppendingString:@"\n\n\n\n"];

if(nil != authenticateAlertView)
[authenticateAlertView release], authenticateAlertView = nil;

authenticateAlertView = [[UIAlertView alloc] initWithTitle:@"Authentication Required" message:messageString delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Log In", nil];
authenticateAlertView.tag = 1001;
[authenticateAlertView show];

if (nil != usernameTextField)
[usernameTextField release], usernameTextField = nil;
usernameTextField = [[UITextField alloc] initWithFrame:CGRectMake(14, 75, 255, 25)];
usernameTextField.placeholder = @"Username";
usernameTextField.backgroundColor = [UIColor clearColor];
[usernameTextField setBorderStyle:UITextBorderStyleRoundedRect];
usernameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
usernameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameTextField.keyboardType = UIKeyboardTypeAlphabet;
usernameTextField.returnKeyType = UIReturnKeyDefault;
usernameTextField.delegate = nil;
usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
usernameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

if (nil != passwordTextField)
[passwordTextField release], passwordTextField = nil;
passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(14, 105, 255, 25)];
passwordTextField.placeholder = @"Password";
passwordTextField.backgroundColor = [UIColor clearColor];
[passwordTextField setBorderStyle:UITextBorderStyleRoundedRect];
passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
passwordTextField.autocorrectionType = UITextAutocorrectionTypeNo;
passwordTextField.secureTextEntry = YES;
passwordTextField.keyboardType = UIKeyboardTypeAlphabet;
passwordTextField.returnKeyType = UIReturnKeyDefault;
passwordTextField.delegate = nil;
passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
passwordTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

[authenticateAlertView addSubview:usernameTextField];
[authenticateAlertView addSubview:passwordTextField];
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900