I have a basic question and I am hoping there is a simple answer that I have missed. The basic premise is that I am going to connect to a website and download some JSON data. I am using the framework provided by Stig Brautaset and the tutorial worked really well.
My issue is that the website I am connecting to is in the format and the username and password are fixed for my app so the user will never be entering them.
curl -u username:password http://website.com/page
How do I pass in the url and password to a NSURLConnection?
It doesn’t have to be an NSURLConnection, it just seems the best option.
I have looked at the AdvancedUrlConnections sample but it seems overly complex and is quite old. Searching on the web hasn’t been much better. I am hoping that one of you guys can say just set these 2 properties, followed by suitable insults of course…
An NSURLConnection will work just fine. As pointed out in another answer, implementing the
didReceiveAuthenticationChallengecallback is quite easy to do.This is all pretty straight forward. It’s in the delegate methods of the NSURLConnection where all the magic happens:
This is where you handle the challenge for credentials. I have hardcoded a fake username and password to demonstrate how it works. I personally have a separate delegate object handle the challenge. Keep in mind that the connection will sit in an idle state until you respond to it or until the connection times out.
You’ll need to implement these other methods for an NSURLConnection, as well: