Using authentication headers with a NSURLMutableRequest in IOS

When you have your app running on your IOS device open a connection to a server, sometimes you will want to have the connection be a secure connection using HTTPS. This blog entry discusses how to setup your NSURLSession and NSURLMutableRequest object to setup the fields in the HTTP headers to open a secure HTTPS connection to your server. In my latest project with Narrative Technologies, the litho reader application required a secure connection to the servers in order to control the access to the users downloads, wish lists, and other items that were unique to each subscriber. We used the HTTPS authentication to control access to these items on the server.

Below is a selector that we use to download something from a server using a secure connection.

So that’s how we setup the NSURLSession and NSURLRequest objects to open a HTTPS connection to your server and process the results in a completion block. The technique is similar when using a background download task in a URLSession. But what do we need to do to create the proper authentication header for this example? For this sample we are using Basic authentication, so the string that we need to send to the server is formatted like this: “Basic ==”. For this example we assume that the server uses your email to track your login user on the server and we then have a password that would have been either assigned or registered on the server when the user signed up.

Objective-C and IOS make it very easy to construct authorization headers for a secure connection