Credentials

When you try to access a web service, it will sometimes respond with an authentication challenge, which means Who the heck are you? You then need to send a username and password (a credential) before the server will send its genuine response.

When the challenge is received, the NSURLSession delegate is asked to authenticate that challenge, and the delegate will respond by supplying a username and password.

Open BNRCoursesViewController.m and update fetchFeed to hit a secure Big Nerd Ranch courses web service. (Do not forget to use https instead of http.)

-​ ​(​v​o​i​d​)​f​e​t​c​h​F​e​e​d​
{​
 ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​r​e​q​u​e​s​t​S​t​r​i​n​g​ ​=​ ​@​"​h​t​t​p​:​/​/​b​o​o​k​a​p​i​.​b​i​g​n​e​r​d​r​a​n​c​h​.​c​o​m​/​c​o​u​r​s​e​s​.​j​s​o​n​"​;​
 ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​r​e​q​u​e​s​t​S​t​r​i​n​g​ ​=​ ​@​"​h​t​t​p​s​:​/​/​b​o​o​k​a​p​i​.​b​i​g​n​e​r​d​r​a​n​c​h​.​c​o​m​/​p​r​i​v​a​t​e​/​c​o​u​r​s​e​s​.​j​s​o​n​"​;​

 ​ ​ ​ ​N​S​U​R​L​ ​*​u​r​l​ ​=​ ​[​N​S​U​R​L​ ​U​R​L​W​i​t​h​S​t​r​i​n​g​:​r​e​q​u​e​s​t​S​t​r​i​n​g​]​;​
 ​ ​ ​ ​N​S​U​R​L​R​e​q​u​e​s​t​ ​*​r​e​q​ ​=​ ​[​N​S​U​R​L​R​e​q​u​e​s​t​ ​r​e​q​u​e​s​t​W​i​t​h​U​R​L​:​u​r​l​]​;​

The NSURLSession now needs its delegate to be set upon creation. Update initWithStyle: to set the delegate of the session.

-​ ​(​i​n​s​t​a​n​c​e​t​y​p​e​)​i​n​i​t​W​i​t​h​S​t​y​l​e​:​(​U​I​T​a​b​l​e​V​i​e​w​S​t​y​l​e​)​s​t​y​l​e​
{​
 ​ ​ ​ ​s​e​l​f​ ​=​ ​[​s​u​p​e​r​ ​i​n​i​t​W​i​t​h​S​t​y​l​e​:​s​t​y​l​e​]​;​
 ​ ​ ​ ​i​f​ ​(​s​e​l​f​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​s​e​l​f​.​n​a​v​i​g​a​t​i​o​n​I​t​e​m​.​t​i​t​l​e​ ​=​ ​@​"​B​N​R​ ​C​o​u​r​s​e​s​"​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​U​R​L​S​e​s​s​i​o​n​C​o​n​f​i​g​u​r​a​t​i​o​n​ ​*​c​o​n​f​i​g​ ​=​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​N​S​U​R​L​S​e​s​s​i​o​n​C​o​n​f​i​g​u​r​a​t​i​o​n​ ​d​e​f​a​u​l​t​S​e​s​s​i​o​n​C​o​n​f​i​g​u​r​a​t​i​o​n​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​_​s​e​s​s​i​o​n​ ​=​ ​[​N​S​U​R​L​S​e​s​s​i​o​n​ ​s​e​s​s​i​o​n​W​i​t​h​C​o​n​f​i​g​u​r​a​t​i​o​n​:​c​o​n​f​i​g​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​d​e​l​e​g​a​t​e​:​n​i​l​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​d​e​l​e​g​a​t​e​Q​u​e​u​e​:​n​i​l​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​_​s​e​s​s​i​o​n​ ​=​ ​[​N​S​U​R​L​S​e​s​s​i​o​n​ ​s​e​s​s​i​o​n​W​i​t​h​C​o​n​f​i​g​u​r​a​t​i​o​n​:​c​o​n​f​i​g​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​d​e​l​e​g​a​t​e​:​s​e​l​f​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​d​e​l​e​g​a​t​e​Q​u​e​u​e​:​n​i​l​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​[​s​e​l​f​ ​f​e​t​c​h​F​e​e​d​]​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​s​e​l​f​;​
}​

Then update the class extension in BNRCoursesViewController.m to conform to the NSURLSessionDataDelegate protocol.

@​i​n​t​e​r​f​a​c​e​ ​B​N​R​C​o​u​r​s​e​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​(​)​ ​<​N​S​U​R​L​S​e​s​s​i​o​n​D​a​t​a​D​e​l​e​g​a​t​e​>​

@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​)​ ​N​S​U​R​L​S​e​s​s​i​o​n​ ​*​s​e​s​s​i​o​n​;​
@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​c​o​p​y​)​ ​N​S​A​r​r​a​y​ ​*​c​o​u​r​s​e​s​;​

@​e​n​d​

Build and run the application. The web service will complete with an error (unauthorized access), and no data will be returned. Because of this, the BNRCoursesViewController will not have an array of courses to display, and so the table view will remain empty.

To authorize this request, you will need to implement the authentication challenge delegate method. This method will supply a block that you can call, passing in the credentials as an argument.

In BNRCoursesViewController.m implement the NSURLSessionDataDelegate method to handle the authentication challenge.

-​ ​(​v​o​i​d​)​U​R​L​S​e​s​s​i​o​n​:​(​N​S​U​R​L​S​e​s​s​i​o​n​ ​*​)​s​e​s​s​i​o​n​ ​t​a​s​k​:​(​N​S​U​R​L​S​e​s​s​i​o​n​T​a​s​k​ ​*​)​t​a​s​k​
d​i​d​R​e​c​e​i​v​e​C​h​a​l​l​e​n​g​e​:​(​N​S​U​R​L​A​u​t​h​e​n​t​i​c​a​t​i​o​n​C​h​a​l​l​e​n​g​e​ ​*​)​c​h​a​l​l​e​n​g​e​
c​o​m​p​l​e​t​i​o​n​H​a​n​d​l​e​r​:​
 ​ ​ ​ ​(​v​o​i​d​ ​(​^​)​(​N​S​U​R​L​S​e​s​s​i​o​n​A​u​t​h​C​h​a​l​l​e​n​g​e​D​i​s​p​o​s​i​t​i​o​n​,​ ​N​S​U​R​L​C​r​e​d​e​n​t​i​a​l​ ​*​)​)​c​o​m​p​l​e​t​i​o​n​H​a​n​d​l​e​r​
{​
 ​ ​ ​ ​N​S​U​R​L​C​r​e​d​e​n​t​i​a​l​ ​*​c​r​e​d​ ​=​
 ​ ​ ​ ​ ​ ​ ​ ​[​N​S​U​R​L​C​r​e​d​e​n​t​i​a​l​ ​c​r​e​d​e​n​t​i​a​l​W​i​t​h​U​s​e​r​:​@​"​B​i​g​N​e​r​d​R​a​n​c​h​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​p​a​s​s​w​o​r​d​:​@​"​A​c​h​i​e​v​e​N​e​r​d​v​a​n​a​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​p​e​r​s​i​s​t​e​n​c​e​:​N​S​U​R​L​C​r​e​d​e​n​t​i​a​l​P​e​r​s​i​s​t​e​n​c​e​F​o​r​S​e​s​s​i​o​n​]​;​
 ​ ​ ​ ​c​o​m​p​l​e​t​i​o​n​H​a​n​d​l​e​r​(​N​S​U​R​L​S​e​s​s​i​o​n​A​u​t​h​C​h​a​l​l​e​n​g​e​U​s​e​C​r​e​d​e​n​t​i​a​l​,​ ​c​r​e​d​)​;​
}​

The completion handler takes in two arguments. The first argument is the type of credentials you are supplying. Since you are supplying a username and password, the type of authentication is NSURLSessionAuthChallengeUseCredential. The second argument is the credentials themselves, an instance of NSURLCredential, which is created with the username, password, and an enumeration specifying how long these credentials should be valid for.

Build and run the application and it will behave as it did before you changed to the secure web service, but it is now fetching the courses securely over SSL.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset