[MUSIC] We've come a long way. We've authenticated with Instagram. We've gotten a callback. We've authorized our application to talk to services on the Internet that maybe aren't the most secure. Now what we need to do, is we need to finish this process off and understand the data that comes back from Instagram. So let's go into our code, and I put the code there and then we're just gonna uncomment it and walk through it as we talk about it. So, for starters, let's look at what happened when we hit the login button. That occurs right here. We've already seen this. We're gonna request access to account with type Instagram, great. And if we do that, we'll need to go back after this and turn on and off the buttons but maybe we can start that off right now. So lets see. So after we load then we want to say self.logoutButton.enabled = false. So you don't want to logout until after you've logged in. And self.refresh.enabled = false, because you don't want to be able to refresh until you've logged in. And then we'll say after we login, we will set both of those To true. Now ideally we would check to see if our login was successful, but we'll just assume that's gonna be successful for the purposes of this one. And self.loginButton.enabled = false. Okay, so we turn ourselves out. Okay, so then when we want to logout, what happens when we logout? Well we need to get access to our shared store, so this is the place where all of our credentials are kept. We wanna get access to all of the accounts in our store for the type of Instagram, because we could potentially be authenticating to multiple different services. And in fact, we might be authenticating to Instagram with multiple different accounts, so we're gonna get an array back of all the Instagram accounts that are authenticated. And when we logout, we're gonna go through each one of them, and we're gonna tell the store to remove that account, so all the Instagram accounts. And then the last thing that we're gonna do is, after we log out, we're going to disable the logout and refresh buttons and enable the login button. Okay, then finally, when it becomes time to refresh our button, this is where we're gonna do all our work. There's a lot of error checking that we have to do here and there's some more services that we need to. Requests from the Internet. So let's start, and I'll show you the code that was run down there with the warning Instagram, one Instagram account logged in. So the first thing that we're gonna do, is we're gonna access our shared store, our source of all of our information about our accounts. And we're gonna ask for all the accounts of type Instagram, I'm using a capital I. We're just gonna check to see if there is an account that has been logged in. And if that count is 0, then we're just gonna put a message on the log that says, telling how many accounts have been logged in. Which should be 0, because we just checked for it. And if that's the case, then there's nothing to request, there's no information to get from the Internet. So we're immediately gonna return and not do the rest of the code. But, if there is at least one Instagram account available, then we're gonna get it. And we're just gonna get the first one and if for somehow there are more than one Instagram accounts registered and I'm not quite sure how that would happen, we're gonna ignore all but the first one. Within that store is the access token that we need in order to request information from our web service. Now, we're going to start to build a URL, from which we are going to request information from Instagram about the user. So, from the API, we get the URL in question, and in this case we want api.instagram.com/v1/users/self/feed. And if we go to Instagram, it will list all the different kinds of data that you can get about this user, and in this case we're gonna get the person who's logged in, their feed of pictures. And what you need to do in order to get that information back, is in addition to specifying this URL, you have to add the access token, which is what we got after we authenticated. So, we're going to create a URL by putting the string literal here, and then adding the token that we got from our store here, and then we're going to build a URL from that string. Pretty straightforward, a URL object. Now, we have the source of this data. We're now gonna go out to the Internet and ask for it, to get it back. To do that, we need to create an NSURLSession. This is the mechanism in iOS that does the request for us on the Internet. We'll get the one shared session for our app. And then we're just gonna make a colleague go out and get the data. Now what I'm doing here, is I'm showing you the pattern for how you make that call. You access the session, you send it which URL you want to request. And then you tell it what to do on response. And then you pass it the resume message in order for it to go. Now, the thing about requesting resources from the Internet, is that it takes time and it takes a variable amount of time. And so, all of the work with what you do with the response is done in the completion handler. In this case I'm making it nil just so you can see the pattern of whats happening here. If we run this code, nothing's gonna happen, the request will go out on the Internet but nothing will come back. Let's run it and just see if everything's working okay, and then we'll add some code into that completion handler. We get a warning because we shouldn't be nil passing now to that completion handler. What's the point, if you're not gonna do anything with the data back? But, it's just a warning, so it should run okay. So we see our logout and our refresh, our grade out. Let's go ahead and login. Okay, I've already logged in, so I'll kick back. Great, and now I should be able to refresh, oh and we see we received a call back, great, we hit refresh. And everything's fine, because we have an account logged in, we're making a request to the Internet but we're not doing anything with the result. All right, so now let's do something with the result. In order to do something with the result, we need to give the completion handler an anonymous function so that the answer that comes back is used. So here's the signature of the anonymous function. It receives a variety of different parameters and here is where we put the code that is going to do something with it. So we're gonna get the data back, we're gonna get a response. And we're gonna get any error codes. Now, this is a little cumbersome because we have to check for three different kinds of errors. The first kind of error we have to check for is a network error. To see if for some reason, the network went down and we are unable to get any data back. The second kind of error we have to check for is an HTTP error. And that's an error if for some reason the Instagram web server is broken. And then the last error we have to check for, is to make sure that the JSON that we get back from Instagram is well formed. Make sure that it parses okay and creates a data object. So I'm gonna go ahead and cut and paste that code from down here and we'll walk through it. All right, so our error handling is gonna be right here. So the first thing that we're gonna do, is we're going to check for a network error, and that's one of our parameters. So if there's an error, then we're simply gonna say we couldn't finish the request and we're gonna return immediately and do nothing else, that's a network error. If there's an HTTP error, and the way we're gonna do that, is we're gonna check the response that we got back. We know that what we just sent was an NSHTTPURL response, we know we're gonna get that back cuz that's what we passed up here. Just a fact that if your HTTP code is less than 200 or greater than equal to 300, then that's sort of the rough way of saying that you got an error. If we got an error, we're just gonna return. And then finally, we're gonna check for a parse error, so we're gonna take that data that we got back, which is right here. So this is a data object that we received, and we're going to try and parse it using our NSJSON serialization, which you may remember from our JSON lecture. And if it parses okay, then down here, we are going to be able to access our data. In which case, we are going to be interested in that URL object. So let's go back to the example that Instagram gave us about what the JSON is gonna look like. And we can see here that there's a particular path we want to follow down to the URL that we're trying to grab. So we wanna find data and then we want to find images. And then we want to find standard resolution. And then we want to find the URL for that. Okay, so let's come back in a second and look at how we're gonna parse out that data, get that URL. And then we're gonna go back out to Internet, again, and ask for the data from that URL. [MUSIC]