views:

277

answers:

2

If the website doesn't require authentication, my code works fine, if it does, it errors with EXC_BAD_ACCESS right after printing "credential created". I'm not releasing anything, and this code is copied straight from the documentation - any idea what's wrong?

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
 if ([challenge previousFailureCount] == 0)
 {
  NSLog(@"received authentication challenge");

  NSURLCredential *newCredential;
  newCredential=[NSURLCredential credentialWithUser:@"root"
             password:@"rootpassword"
             persistence:NSURLCredentialPersistenceForSession];


  NSLog(@"credential created");

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

  NSLog(@"responded to authentication challenge");

 }
 else
 {
  NSLog(@"previous authentication failure");

  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authentication Failure" 
              message:@"Website did not accept the username and password."
                delegate:nil
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil];
  [alert show];
  [alert release];
 }
}
A: 

The only thing you're not controlling is the return value of [challenge sender]. Try printing its description to ensure that it's non-nil.

Jeff Kelley
A: 

Turns out it's something to do with that particular sites authentication method - replacing the website I want to log into with another website and the code works fine.

(I was trying to scrape data from unRaid's status page, unRaid doesn't use an actual webserver but emhttp so I'm assuming that has something to do with it)

BarrettJ