I need to invoke an initial GET HTTP request
with Basic Authentication
. This would be the first time the request is sent to the server and I already have the username & password
so there's no need for a challenge from the server for authorization.
First question:
Does
NSURLConnection
have to be set as synchronous to do Basic Auth? According to the answer on this post, it seems that you can't do Basic Auth if you opt for the async route.Anyone know of any some sample code that illustrates Basic Auth on a
GET request
without the need for a challenge response? Apple's documentation shows an example but only after the server has issued the challenge request to the client.
I'm kind of new the networking portion of the SDK and I'm not sure which of the other classes I should use to get this working. (I see the NSURLCredential
class but it seems that it is used only with NSURLAuthenticationChallenge
after the client has requested for an authorized resource from the server).
Since NSData::dataUsingEncoding is deprecated (ios 7.0), you could use this solution:
I'm using an asynchronous connection with MGTwitterEngine and it sets the authorization in the
NSMutableURLRequest
(theRequest
) like so:I don't believe this method requires going through the challenge loop but I could be wrong
Even the question is answered, I want to present the solution, which doesn't require external libs, I found in another thread:
If you are using GTMHTTPFetcher for your connection, basic authentication is fairly easy as well. You simply need to provide the credential to the fetcher before beginning the fetch.
If you don't want to import the whole of MGTwitterEngine and you aren't doing an asynchronous request Then you can use http://www.chrisumbel.com/article/basic_authentication_iphone_cocoa_touch
To base64 encode the Username and password So replace
with
after
you will need to include the following file
Here is a detailed answer with no 3rd party involved:
Please check here:
Kindly let me know your feedback on this.
Thanks