Question

I'm a little confused by this. I have this method.

-(BOOL)verifyAuth: (NSString*)username forPassword:(NSString*)password
{
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLCredential *creds = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession];

    [session dataTaskWithURL:MALAuthVerifyURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
    {

         NSLog(@"%@",data);
    }];
    return YES;
}

How do I pass the credentials to the NSURLSession object? Apple's docs say this:

"After you’ve created the NSURLCredential object:

For NSURLSession, pass the object to the authentication challenge’s sender using the provided completion handler block."

But i'm not sure what this actually means. I can't find any examples of this usage.

Thanks.

Was it helpful?

Solution

https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW1

HTTP basic authentication (NSURLAuthenticationMethodHTTPBasic) requires a user name and password. Prompt the user for the necessary information and create an NSURLCredential object with credentialWithUser:password:persistence:.

After you’ve created the NSURLCredential object:

  • For NSURLSession, pass the object to the authentication challenge’s sender using the provided completion handler block.
  • For NSURLConnection and NSURLDownload, pass the object to the authentication challenge’s sender with useCredential:forAuthenticationChallenge:.

Maybe this helps.

OTHER TIPS

You'll need to implement another delegate method, -URLSession:task:didReceiveChallenge:challenge:completionHandler:.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top