Authenticating With Web Services

April 07, 2012

Reading time ~1 minute

Whether the web service you're working with is using HTTP Basic Auth or HTTP Digest Auth, as long as you're using NSURLConnectionNSURLCredential has you covered.

Apple have made it very easy for us developers. All we have to do is implement a delegate method.

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge previousFailureCount] < 0)
    {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        return;
    }

    NSURLCredential *credential = [NSURLCredential credentialWithUser:aUsername 
                                                                 password:aPassword 
                                                              persistence:NSURLCredentialPersistenceForSession];
    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}

And that's it. NSURLCredential will handle both HTTP Basic and Digest auth for you, including all the ugly base64 encoding and MD5 hashing.

The Day I Was Hacked

It was 4am on a Saturday in 2013 and I was sleeping. My iPhone was sitting on my bedside table, plugged in in silent mode. It buzzed once...… Continue reading

Siri Remote - The Future of Gaming?

Published on September 20, 2015

Open Source Is Not 'Do What You Want'

Published on April 29, 2015