[MUSIC] We're now gonna step through what may be the most complicated part of this wiring of our app together. Because what we're gonna do, is we're going to try and include both the information about our URL scheme, register our application to get those things. And we're also gonna try and put the information about our OAuth 2.0 authentication into place. So let's do that. Now, the first thing we need to think about a little bit differently is that when we authorize our application to access Instagram. That's something that happens on an application-wide basis, not just within a UI. So because it's happening on an application-wide basis. We're gonna put the code in this file here, AppDelegate.m. Here's where we put code that affects everything in our application, not just the user interface. You can see that X codes created a bunch of template code for us and that's great. What we wanna do is we wanna add our code to initialize our authentication here in our did finish launching with options method. This is what gets called at the end of our application's launch part of its lifecycle. We want to initialize our nx to library. In order to do that what were gonna do is were gonna go look for the documentation for that coco pod. To try and see, what it recommends, how to do that. So, were gonna open a browser. I've already found it here but just to show you how to find it, if I was going from scratch. I would look up NX OAuth 2.0 to coco pod. And I would go to the git hub, which is where the source code forward is stored. I would scroll down, getting started. Don't wanna include the manually include the library, i'm using the coca pod that's not manually. Now, using the OAuth 2.0 client, configure your client. This is what I want. Great, so this is basically the code that i'm gonna take, but take a look at the wiki for some examples. So I'm gonna do that. The Wiki shows an example for Instagram, that looks good. Oh, here we go, this is the code I want exactly for Instagram. So, I'm gonna copy it, and I'm gonna put it here in my initialization code. And then I'm gonna walk through what it does. Well for starters, there's this class called NX OAuth 2.0 account store, which we're getting the shared store from. A singleton class keeps all the information fro this object or application one spot but doesn't recognize it. Hmm. How do we get code to recognize a class that we know is in our project, cuz we reported it with a coco pod but for some reason is not available to our file. That's because we need an import line. We come up here and we add an import to NXOAuth2.h, which is a file that is located down here in our pods. We should see that we are then able to access this This class. It looks at like that errors gone away, but we've got a new on here. Well let's see what our error is and then we'll go on. No known instant method for secret scope authorization. What does it know? Let's see. Well, let's see if we can figure it out from code completion. NXOAuth2AccountStore sharedStore setClientID. Okay, we're gonna set secret, we're gonna set authorization URL, set our token URL. Okay, so it looks like that's good. It's just, it doesn't know scope. All right, so let's remove scope, see if that passes. Okay, great that looks better. But we know that we have this client ID and we know we have this secret, it's not just generic. These are the things that we got when we registered as a developer. So let's go grab that from our notes. Client ID is what we got when we registered our clients so we'll put that there and our client secret was when we registered we put that there. And our authorization URL, well is this right? Well, what you can do is if you want to verify this and you want to use a different API, you can go to the documentation for the API and find this. So the Instagram documentation If you look under Authentication, you see, oh, it want's the authorization URL. So, we look for the authorization URL. There it is. Direct the user to our authorization URL. You can see that it's located right there. There it is. Authorized, great. And if we wanted the access token URL We'd find it, here it is, right here. And we put it in right there. Then lastly, for the call back, we need to use the call back that we registered with our application. So we registered scheme at thing.com because I was having trouble with the web forum and that's what it ended up being. Great. And then this last thing here is just some way for us to identify the servers that we're working with later. All right, so if all goes well. That's gonna initialize the NX OAuth 2.0 client with the information that it needs in order to get our app to be able to use Instagram. All right, that's fine. But now we actually need to cause our application to go out and execute that initialization procedure, and that's gonna be triggered off our log in button. So let's go back to our view controller. And we're gonna, when the log in button is pressed. We're going to want to call our NXOAuth2. What was the code we used before AcountStore. Actually we just kind of want that. Want to access our shared object. And we want to. I want to figure out what we do in order to request their credentials. So I'm gonna go back to the Documentation like I would with anything that I'm not sure about. And I would say, okay if we come back here, we'd say configuring your client. Requesting access to, oh, request access to account. There we go, great. All right, so we're not gonna do it with the username and password. We're gonna do it with our external browser. So if we come back here and we use that pattern, requestAccessToAccountWithType, okay, say, requestAccessToAccountWithType. And then we give it that string that we listed before, Instagram is the account we're working with. All right, and then that should if all goes well, that should initiate do I still have an error? What's going on? Use of undeclared identifier. Oh, that's the problem again. We have that class problem, how do we solve that? That's right, we need to import the code from our CocoaPod so that our file is aware. All right, now we're aware of it, is that going to fix it? Great. All right, so now, after our application is done loading. We initialize our NXOAuth 2.0 account store with the secrets that we need for accessing Instagram. When we hit the login button, we're gonna go out. And we're gonna use the browser to login to Instagram. The next thing that we need to do is, we know that after that happens, Instagram is gonna call our application back with a URL. And we need to let X Code know that our application is one that receives that particular URL. Okay, so that's what's called a custom URL scheme. So if I wanted to find out more about a custom URL scheme, what I would do is I would go look at the developer documentation for custom URL schemes on Apple. So let's see, custom URL scheme. I'm showing use this so that you can have some idea of how you would go about trying to find this stuff out. Inter-app communication, and we're looking for custom, URL scan, there we go. Sending URL to another another app? No, registering custom URL schemes, this is exactly what I want. All right, so it says, in order to do this, we have to change our info.plist file. And we need to set some keys and some dictionaries and some URL application support in our info.plist file. Okay, well we can follow along and do this. So let me just put this side by side so we can read our. Okay. There we go so we can read it, same time. So we're gonna alter info.ps list. All right. So this is part of the manifest that comes with our That comes with our applications. So let's see. Let's see if we can make a little space here so we can. Okay. It says that, to register a URL for your app, include the CF bundle URL types key in your app's info.plist file. All right, so one thing that you might not know, and this is something you have to learn. Is that if you right click on here, you can show the raw keys and values and that is what we need in order to add this particular URL type. So if we add a row, and we know it says we need to add CF Bundle URL Types, so to register a URL type your app, include the CF bundle URL types. Great, key, and your info.pls file .infocfbundleURLtypeskey contains an array of dictionaries, so that's gotta be an array, great. All right. We each switch to find a URL scheme the app supports, okay? So now table 6.1 describes the keys and values to include in each dictionary. All right, so here's our first dictionary. And oh look, it's already pre-filled for us. Okay, so bundle URL name, a string containing the abstract name of the URL scheme. To ensure uniqueness, it's recommended that you specify the reverse DNS style identifier. Hm, interesting. That stringy specifies also used as a key in your house info dot pls., string spa. All right. So, what were gonna do is were going to go ahead and get whatever our applications general information are bundle like data fires. We're gonna grab that right there come back to info key list. URL type, let's look at raw again so it matches our documentation. Okay, we see that that CF bundle URL name needs to be that string. So, you need to set that equal to the value it's got. Great. And then Second key is CF bundle URL schemes, an array of strings containing the URL scheme names. Okay, so let's see we need to add something else. And it's CF Bundle URL scheme. All right, let's try that. CF bundle URL schemes. Great. And it's array, okay so we're gonna add one element to that, it's gonna be a string. And what we're gonna add is we're gonna add scheme. Now scheme was what was at the beginning, if we go back to our notes, what was at the beginning of our redirect URL. So if all goes well, what's gonna happen is when Instagram calls back with the scheme At the front of the URL, our app will be registered to receive that and then we'll be notified. Okay, so that's good. I think that's what we need at this part of our application. And so now if we go maximize our window again. If we go back to our app delegate, so how do we specify? What it is that we want our application to do. How do we get that signal back? We know that iOS is gonna be looking for us when the scheme protocol gets called. How do we actually receive that information and do something? Well, let's see. What we're gonna do is we're gonna go to AppDelegate.m. And there's a standard function that you use to receive URL scheme calls. And hopefully it will come up automatically, application. So this is what that library is doing for us. It's going to handle it and by handling what that means it's going to do is going to log us in and then give us access to that account if we need it. All right, so this is the login flow. And just to see if that flow actually happened, I'm gonna go ahead and log. Use NSLog to log to the console. We received a callback. All right. I don't know why it's, strange capitalization. Okay. How's that? All right, so when we run this, let's just walk through this now. When we hit login, when we hit our login button, what's going to happen? We're going to ask our NXOAuth2AccountStore to log in. If it all goes well, it's gonna kick us to the Safari browser. Safari browser's gonna ask us via Instagram to log in. That will kick us back using the scheme protocol. We will receive it and we will log that message. All right, let's see if it works. It's a pretty complicated sequence of actions. Running on an iPad Air 2, because that's leftover. All right great, so let's log in. Okay, switched to Safari, that's great. We're at Instagram, that's terrific. Log in with my account [SOUND] this is great so Granny Plus is requesting to do the following, access your basic information. Yep, that's great. I'm gonna let it do that. Okay, so now what's happened here is the reason why it's asking me to open in GrammyPlus is because it's looking for that URL scheme. URL scheme's coming back, I'm gonna open it. Great, something good happened, cuz I'm back now in my application. Did I get an error, what happened? We received a call back. Great. And then something failed, but I'm actually not that worried about it because I got as far as I had planned. So that's really great. So what we saw here is we saw that we were able to connect our authorization. We're able to log in and so now what we need to do is we need to request the information from Instagram to fill our image. So that's our next step. Probably passed the hardest part right now. [MUSIC]