Login To Your Database | Swift 5, Xcode 10

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on everybody it's your boy kill'em oh and today I'm gonna be showing you how to log in to your own custom back in database using URL sessions now your back-end might be you know code it in PHP go JavaScript hell it might even be coded in Swift that's what my back in this code didn't so I'm actually gonna show you how to log in you already have this user all set up and signed up and I'm gonna actually show you how to log in assuming that all you need to do is make a post request to your database and that will give you all the information you need for your app to log in and go to the main part of the app so as you can see we're gonna be working with this general set of just your your typical login screen where it has a username and a password and you know the login button so let's jump in but before we jump in I just want to say make sure you head over to Kela loco comm if you haven't already and see if there's anything here that might be a value for you maybe a course maybe the all-access membership where you get access to me via slack you know or maybe you just want to help out the channel so make sure you check that out and let's jump right on into it so as you already saw we have our UI set up and it's just a simple table view controller and this is our this is our login view controller that's going to be managing all that for us now as you can see we already have the username and the username and password text field set up and we also have the login button set up and in the login button functionality right now all we're doing is unwrapping the username and password and we're just printing them out so if I were to jump back over to the app and enter in my information and now that I have my information entered in and then I hit login we'll actually be able to see the credentials so we have Kela loco as the username and password to 3 as the login now if you want to use my back-end to practice this you most certainly are welcome and let me show you that I actually have postman pulled up right here which is a you know just a tool to do you know networking requests forget post update delete all those other things so as you can see this is my bass or this is my end point right here Kela local Heroku app comm and if you do slash API slash users you can see all the users now I only have one user in here and as you can see it's giving us back all the information the ID the username the age name and password now normally you wouldn't give back the password but in this case we are trying to log in and we need an example so I am giving back the password so if I were to run a post request on my login endpoint right here as you can see it's slash API slash login all I have to do is make sure that I'm entering in the username which is Kela loco the password which is passed one two three and then when we send that we're gonna get back a user object and our user object we should be expecting to get back without the password included so I did that as well so let's go ahead and look at the what we have for our actual app right now and as already said that's what the login button is going to eventually be doing and then lastly we just have a segue since we're going to be using storyboards on this one just so that we can demonstrate that we're going into the main part of the app so there are a couple of other things that we have in here the alert service which is just going to handle us doing our handle doing the pop-ups for us and then we have a networking service which actually doesn't have anything in here and then if you guys want the started project link is in the description you can download it for free this is where we're gonna be writing all of our logic the the last thing that I just want to show you is that we do have a user object and I want to state that this is a decodable object so we will be able to decode our response into a user object when we get there so let's jump over to the networking service and now let's talk about what we expect to go on in here so for our networking service we're expecting to be able to obviously do networking we're gonna be making a request that it's gonna go out to the internet it's gonna hit our endpoint and then it's gonna its supposed to return back the response so what we want to do is we want to create a function that's going to be called request so let's do that and before I go any further I just want to say that we are gonna actually end up with two requests functions one to do two the two different types of post requests that you would normally come across we're gonna do a form data request and we're also going to do a JSON request so the form data requests we're gonna work on first and when we have a request we need to be able to return something in a closure essentially because the code that we get back we're gonna we're gonna go out to the internet right we're gonna and we don't know when that code is gonna come back we don't know when the API is gonna send us a response back and by the time it sends it back this function will have already finished executing because the networking request is done on the background thread so what we need to do is we need to make sure that we have a closure that is going to be able to still run code even after this this function has finished running all right so that so what we ended up doing was we have a completion you know we have a argument called completion which is taking an enclosure and the at escaping is essentially specifying what I just said where we don't know when the request is gonna come back it might take you know three milliseconds it might take three seconds it might take thirty minutes who knows so once it does come back we need to make sure that it that we're able to run this closure and we know that this closure is going to happen sometime after this functions already finished now another thing that we need to know is what endpoint are we calling so we need to make sure that we pass that in as an argument as well so we know that we need an endpoint and we know that we need to have a completion Handler or closure to pass the data through whenever we're calling this request function which is going to be called in our view controller now one thing that I also forgot to mention is that we're gonna need to have something be sent back to us wherever we're calling this request function which like I said it's going to be in the view controller we need to have it send back that data the information that we want so I'm actually going to make sure that inside of the closure the argument that's being passed in is going to be a result and results in Swift five which you're going to see has two generic parameters which we can pass into it so let's add that now alright so as you can see we want a result that's going to return back a user in the event of a success but it's also gonna return back error in case you know we run into an error right now if you're using anything below Swift five what you can do is you could create a result type enum that will essentially represent this what I'm working with since this is Swift five so let me show you how to do that real quick alright so now this would be essentially what you could replace um result with you can make it my result or you could call it result yourself if you want since you don't have access to this object since it's only available in Swift five but essentially all you would have to do is put my result right here and you'd still be able to follow along exactly what I'm doing so but I'm gonna go back to using result because I am using Swift five and let's talk about what's going on right here so my result has two generic parameters the first one can be anything and the second one is going to be II which is going to be of type error so whatever we pass into a failure has to conform to error so on a success we can pass through anything failure we have to pass through anything that conforms to error and that's essentially the same thing that result is doing right here all right so we're good for now we're still gonna add a couple more arguments as we go but right now this request is looking good enough so that we can actually continue now another thing that we're gonna need is a base URL so in this case my base URL as we saw over here in her own in postman is going to be essentially this HTTP colon slash slash tequila logo Heroku app comm slash API so that is gonna be my base URL and I just want to make sure that I always have access to that in my networking service alright so now that I have my base URL I can access it anywhere inside of this networking service and you might choose to use your base URL somewhere else or maybe even possibly hide it but for the sake of examples this is gonna work okay so next what we want to do is we actually want to make a actual URL and a URL object we don't want to just be working with strings because in order to do a networking request we have to use an actual URL so let's do that so now that we have a URL object we can move forward but the problem is is that this URL is actually whenever we initialize it with the string it actually returns back an optional URL and we don't want to work with optionals we want to be able to handle this and make sure that the URL is an actual URL and I don't like to force cast it because that's just that's nasty but and I mean it should work but it's just it's just better to do this do it this way so what I'm gonna do is I'm gonna create an error that's specifically designed for my networking service is gonna be a networking error and I'm just gonna return that as a result for my completion if this URL is invalid so let's go ahead and add that now all right so now we're properly handling the URL we have a guaranteed URL and if you guys are a little bit confused on what's going on here make sure you watch my error handling video and I go into depth on how all this is kind of playing now all right so let's move forward now that we have the URL we want to actually make a request with it so let's go ahead and make a URL request all right so now that we have our URL request object this is actually where we're gonna start plugging in a lot of the different things that is gonna specialize our request to be the way that we want it so we have this request and what we want to do is we want to specify okay what are the parameters that are gonna go into this request well in order to actually create parameters I have to create URL components I had to pass in all the parameters to it and then I have to apply it to the body so let's break that down step by step the first thing that we need to do is we need we need to make sure that we're passing in parameters to our our function up here alright so now we have parameters being passed in as a as an argument to our function and what I'll do is after each of these commas I'm gonna just do a line break just so that we could clean up and read it a little bit easier because we're still gonna add a couple more in a couple more arguments to this request so now that we have the parameters what we need to do is we need to break it down so that we can turn it into data and the way that we're going to do that is using URL components now URL CIM URL components have a property on it called query items so a query the query items essentially allow us to say what are all the parameters that are associated with this with the with these components and what the what we want to do is we want to create query items URL query items plug them into the components because that's actually going to give us a query string and that string is what we're going to turn into data all right so let's break down what's going on here because there is a whole lot happening so as you can see we have the components which is created from URL components and now we have this empty array of query items right so we're just going to create an empty array because we're gonna actually loop through all the parameters that are being passed to our request function so all these parameters we need to loop the room so we're gonna loop them right here so for key value in parameters so this is going to be the key or the name of the of the it's gonna be the left side of the argument of a dictionary essentially right so the key is going to be this side the value is going to be this side and we're just looping through each of them so for key value in parameters because this is a dictionary all we need to do to create a URL query item is to pass in a name the name is gonna be the key the value is gonna be the value but it has to be a string value you cannot pass in like a number or a boolean or anything so what we're gonna do is since we don't know what type of value it's gonna be because it could be any value we're going to be using the string initializer that is called describing so essentially it will turn any value into a string appropriately so now we have a URL query item that's being created we're adding that query item to the query items up here so as we loop through each of these parameters we're gonna essentially be turning them into this query item object and then appending them to this query items array right here once we do that like I said the components have a query items property on it which we which we're just setting to the query items that we just created right here so we're setting those to the query items property on the components and once we do that as I also said the the components has a property on it called query now query is of type string and what this does is it formats the the query items into a query string which is used in in networking so let me give you I an idea of what that looks like so this is essentially the string that would be printed out if I was gonna say my username the username prop the username is equal to Kela logo and the password is equal to pass one two three this is the query string that we get out of it and that's what we need to convert to data so now that we're converting this string which is this query right here to data using utf-8 which is the the standard encoding for making requests on the internet we now have this query data now we can actually add this to our request so that's the main part of what you need to do when whenever you're doing foreign requests is essentially go through this process of creating components query items and then getting a string to turn into data so that we can set those as the parameters inside of the request I hope that makes sense let me know if I'm if I'm really bad at explaining things and making it too confusing for you guys alright so now what we want to do is we also want to add in a couple of other things to our request object things such as what type of requests this is so is it a get request post request update delete which one is it so we have a property on request called HTTP method which is which accepts a string and for this example we're just going to make sure that it's always a post request so let's add that so depending on what type of HTTP method you're using you just need to spell it out in a string and make sure that it's all capitalized because I'm having it as a lowercase post would not work the last thing that we want to do is we want to format our request to say hey this is the content type so we need to add a header to our request to let the API know what kind of what kind of request we're actually making so let's go ahead and add in the content type into our headers for the request so this is the way that we would add headers and if you had multiple headers what you could do is you could add in multiple you know you could add a like argument up here that says headers and then we could essentially loop through them and do kind of a similar thing that we did right here so where we would have key and value in headers we can loop through it add in the value right here and add in the the key right here and that would be how you would add multiple headers but in this example we actually don't need any additional headers so this will actually work out quite nicely also notice that this is using the same type of the same type of header content type as our postman request which you'll actually see right here we're using xww form URL encoded as opposed to form encoded which is usually used to pass files like images and stuff like that or raw or and binary I don't exactly remember what those ones do but this is the one that we want to make sure that we're passing so that's just gonna be prefixed by the word application slash and then whatever encoding that you plan on or whatever type of request this actually is for the content type so this is everything that we need to do in order to set up our request but now what we want to do is we actually want to be able to you know send that bad boy out into the world and we want to get back all of the information so let's go ahead and actually create a data task which is essentially the object that that is in charge of doing the networking piece and it's going to go out to the internet so let's go ahead and create a data task and enter in this request to it alright so as you can see I created a reference to our URL session the shared URL session which is perfect really fun for you know most basic you know networking requests and on session it has this this function called data tasks and what it does is it accepts a URL request so we're going to pass in our URL request which is what we have right here and then it also has this completion handler which we're just going to press enter on so that it can format the the closure properly for us so then we're going to be returning back data response and error so let's give those names now all right so now that we have data response an error which are arguments passed into this closure we can go ahead and use them down here now what I usually like to do is you see how it's highlighting tasks and saying that it's been initialized but never used in yellow I just like to make sure that I say task dot resumed and the reason for that is because that's what actually kicks off the networking request and sends it out into the internet if you forget this step you're essentially not going to be making the networking request you'll just be sitting there waiting for for something to happen and nothing will happen so I like to do that before I go in and add all the logic from here what we want to do is we want to check the response and what we want to do is we want to cast this response to HTTP URL response and what that's going to allow us to do is check what what our status code is on the on the API request did we get a 404 page not found did we get a 500 server's down did we get a 200 which means that hey everything went all gravy and your you're good to go so what we want to do is we want to check this response and make sure that we're getting the correct values all right so for if for any reason we can't actually grab the response and cast it to a HTTP HTTP URL response then we're just going to run into this block right here which is gonna do a completion and we're gonna just say that it was a failure and that we have a networking error essentially a bad response which I add it into our networking error as a case just right now now remember that when you went out to the internet and you came back that might have happened you know point three seconds ago it might have happened three seconds ago we don't know when that happened and all this stuff is happening on the background thread whenever we return we want to make sure that we're going to be essentially working on a main thread and I'm going to just make sure that it's on a main thread whenever we kick back the data in our results so what I'll do is I'll wrap everything that we're going to be doing in a dispatch queue to make sure that we're essentially coming back to the main thread all right so now moving forward we're always going to make sure that anything that's returned in our closure is going to be passed back to the main thread because if we didn't it would still be operating on a background thread and that means if we were to try to update the UI inside our wherever we're trying to inside of the closure of wherever we're calling this request what would happen is we would get like weird behavior because it's trying to update UI from a background thread which you're not supposed to do I think I think app will cause it illegal so next what we're going to do is now that this is an unwrapped response what we want to do is we want to check the status code and make sure that it's anywhere in between 200 and 299 if it's anything other than that we're going to consider it an error and we're gonna pass back pass it back as an error as well all right so as you can see we're just doing a switch statement on the unwrapped response we're doing it based off of the status code the status code is actually an integer so that gives us the ability to do switch cases using number ranges so what I'm doing is I'm saying if it's anywhere from 200 all the way up to but not including 300 that's what this that's what this operator does is up to but not including then we're gonna say success if that is not the case then we're just gonna say failure now at this point we have enough in our actual response or in our our request function to actually test this out and see if it's going to work so let's go ahead and go back over to the login view controller and let's go ahead and try this request out so all I'm going to do is I'm going to use my networking service right here and I'm gonna actually create parameters out of this username and password pass it in to our networking service dot request and fill out that request function alright so as you can see I have the parameter set up it's just a dictionary of username and then username and password and password whatever is typed in up here you know that's why we're unwrapping it there and what we want to do is we want to pass in the endpoint which we know is slash login we know that the parameters are obviously the parameters that we just created right here and then when we get to the completion handle we're just gonna press enter so that it could format our function for us and we're just gonna say that the result we're gonna we have to give it a name I'm going to call it result so from here we're all set all we want to do is just print out the result which if it's successful we actually won't be printing anything because we haven't had the success case set up in our networking service right here as you can see there's only failure cases like right here if we can't get the response back or if we have like a bad URL or something so we actually don't have a successful case but what should happen is we should get if it's in between 200 and 300 we should get back a success and if it's not then we should get back a failure I'm also going to print out the status code of the unwrapped response just so that we can see exactly what that status code is so let's go ahead and run it and see what it looks like now so first things first all I'm gonna do is I'm gonna go back over to Xcode I'm gonna make sure that there's nothing in the logs which for there isn't and we're gonna hit login now obviously there's no username or password state it right here so when I hit login we should get a failed request so as you can see we got a 400 failure so it's printing out the status code and we're gate we're printing out just failure right here and the reason for that is going to be quite simply because we didn't enter in the correct information now if we go ahead and enter in the correct information of my user I just entered in kylo loco and pass one two three let's go back over here let's clear this let's press login and now let's see what comes back 200 success so now we know that we're going out to the Internet we're getting back the response and we're actually handling everything properly so far so we're able to get a success case we're able to get a failure case that's perfect so now that we can kind of get an idea of where we're at on the API whether we're getting a success or a failure now what we want to do is we want to handle the rest of the information that's being passed into our closure so for the data and the error we want to be able to handle these now depending on your API I think it can send back an error but but I think the majority of the time this error is mainly gonna have to do with a URL session and it being able to connect to the internet so if your API is sending back in error it technically was successful it was a successful request but I don't think that that error would actually be coming back under error it would still be coming back as data so this is the way that my API is formatted everything always comes back under data regardless of whether it's a 400 or 500 or 200 it's always coming back under data so that you can work with the message that's coming back so what I want to do is first I want to check to make sure that if there is an error then we need to just stop right there and do a failure completion so let's add that in all right so as you can see I'm just simply unwrapping the error passing it into our completion as a failure and we're returning after at that point there's no need to move forward because we hit an error we know that there won't be anything after that now let's go ahead and do a similar thing with the data so now that we're unwrapping the data what I want to do is I need to try to convert this data to JSON so that we can see if we're actually what what object we're actually getting back so let's go ahead and try to convert this unwrapped data to JSON if we can't convert it to JSON then let's go ahead and pass another error all right so as you can see we're wrapping it in a do catch block and we're just gonna pass in if or what we're gonna try to do convert to JSON if that fails then we're gonna just pass in that error saying that we weren't able to convert to JSON that's already given to us by default in the catch block and if we are able to do it then we're able to print that JSON so let's go ahead and try to print the a JSON all right so the first thing that I want to do is I'm gonna go back over to Xcode I'm gonna open up the debug console and what we're gonna do is we're just gonna enter in login without actually entering in a username or password and we should be getting an error back so now let's go ahead and take a look so yes four hundred failure failure which is so we get the four hundred which is being printed by the unwrapped response we're getting failure which is being printed out because four hundred doesn't fall in between two hundred and three hundred and then we are not gaining a unwrapped error we're actually getting this down here as I said it before my API sends back JSON regardless so if it's a error then what we're going to do is we're still gonna send back JSON and it's gonna be formatted like this and if it's successful we're still gonna send back JSON but it's gonna be in you know whatever the api's expected format is going to be so let's go ahead and do a successful request by using the correct credentials I'm just gonna throw away all that garbage down there and we're gonna just go ahead and log in properly alright and now that we have our username and password correct we're gonna log in and we're gonna see that we're getting 200 once again status code we're getting success and we're getting back this object which is going to essentially be a user object so now what we need to do is we need to try to unwrap our object and we need to essentially do that so that we're either getting back a user or we're gonna get back at error so before we continue let's go ahead and make sure that we're we're getting an incorrect object right so Kela logo with the 0 at the end that's going to give us a failed and let's take a look at our JSON so our JSON is giving us back a you know two properties error which is is a number and a reason which is a string so let's go ahead and create a object that this can be decoded into so that we can pass it through our completion handler up here all the way at the top right here so we want to be able to pass an error so let's go ahead and open up a new file and create a error response so as you can see we have this error response which is going to conform to the decodable protocol and it's essentially going going to allow us to take this JSON right here convert it into this object now what we want to do is we want to make sure that this error response can actually be converted to a real error so what I'm gonna do is in an extension I'm gonna add a property to it a computed property which is going to return an NS error so that we can pass that through to through our completion handler so let's go back over to our networking service and let's go ahead and try to attempt to turn this into a user object and if we can't turn it into a user object then let's go ahead and try and turn it into a error object and if that fails then we need to make sure that we're we're going to essentially throw it into the catch block all right so as you can see where we're going to try to convert this this unwrapped data into a user object using the JSON decoder now I want you to pay particular attention to the try optional this means that we're going to attempt to do this but if we can't then what's going to happen is since it's part of this if statement it's actually going to fail and it's gonna run this code now I also want you to pay attention to the fact that I actually didn't use an optional try right here because if it can't create a user it better be able to create an error response object and from the unwrap data using the JSON decoder because if it can't do that then we have real problems and what's actually going to happen as is that this code will now run since this is just a standard try so if this tries and it fails then this catch block will actually catch it and it will return back the response saying why it was unable to decode this error response so now we've done essentially everything that we needed to do to handle all the decoding and the request of our of our request function so let's break this down a little bit one more time as we're creating a URL we're creating a request from that URL before we do anything with the request we need to create components because components has query items on it we're gonna loop through the parameters to create query items from those parameters add that to our query items array we're gonna set that array to the query items property on the components from there we're gonna convert those those query items into a string so we're converting it into a string right here and turning that right here and we're turning that string into data that data will then be passed into the HTTP body as parameters and then we're specifying what type of HTTP method we're using we're specifying the headers and then we're the data task which is essentially going out to the internet we want to make sure that we're always on the main thread when we are using any of this data so we're going to wrap it in dispatch queue we're going to check the responses just so that we can see what's going on we're gonna check if there was an error if so we're gonna pass in that completion handle they're saying that it failed then what we're gonna do is we're gonna try to unwrap this data make sure that there is data on it if we can turn that data into JSON then we should be good to continue on and try to turn it into a user if we do succeed at turning it into a user we're done we're good we can call this completion handler right here if we can't turn it into a user we're gonna try to turn into an error response if we can't turn that into an error response we get the catch block right here if we can turn it into an error response then we're gonna create that error response we're gonna have that error response turn into an actual error which we can pass through to our completion handler as a failure so I hope that does make sense so it once again if if any of this is confusing just reach out to me on youtube and I will definitely try to explain it a little bit better so now we have our result back over here and what we want to do is we want to switch on this result and do different things if we're able to get the user object then we want to be able to do our segue if we're unable to get our user object then we should be getting back an error and we want to use our alert service to present that error and remember since we're going to be in a closure we're going to be using a weak reference to self all right so as you can see I use the capture list just to make sure that we're capturing a weak reference to self and then right here we're using our result because remember that our result is an enum or switching off of it so in the case of a success where you should be only passing through a user so we're going to just call that let user and if we're able to create that user then what we're gonna do is we're just gonna call our segue mine is called login segue and we're gonna pass this the user as the sender and since we're passing it as the sender in our prepare for segue you'll actually see that I'm making sure that we're going to the main app view controller that's the our segues destination and that we're unwrapping the sender as a user and then we're just simply setting that main app VC to user now if we do get an error for whatever reason then we're just gonna go ahead make an alert and we're gonna present that alert in our view controller so let's go ahead and run this and let's see if everything's working as we expect it to all right so the first thing that I want to do is I want to just go ahead and try to log in without passing any information and we're actually getting back an error so the data couldn't be read because it isn't in the correct format so we're getting back something now if we go back over to our console we should see that it's having the same error now notice that the data type couldn't be read because it isn't in the correct format is not the same error that we're getting back we should be getting back username or password is incorrect please try again so I think there is a problem with actually the error response and I I just checked it right now because I wasn't really sure why it wasn't working so what we want to do is we actually want to delete this error property off of it because it's giving us some problems and I'm just gonna set the code to zero so now let's go ahead and run that and see if it works this time alright so now we're gonna go ahead and try it we're gonna log in and we're getting closer to the error that we want back so error or error username or password is incorrect please try again so we're getting closer but it has all that junk so let's go ahead and clear that up what I'm actually going to do is I'm going to delete this this extension and I'm gonna make this struct actually conform to the error protocol itself so that we can pass it directly so we want it to be an error but we want it to be a localized error and localize error it has a property on it called error description so all we want to do is we just want to return the reason and if we do that now what we need to do is we need to go back over to our networking service and instead of returning that error value we're gonna return the error response itself and I think that should work we should be able to run everything and see the error description that we were expecting from the first place so let's go ahead and do log in and as you can see we're now getting back the correct error response username or password is incorrect please try again so perfect so let's go ahead and add in my credentials and let's see if we're able to log in correctly so we have akela logo passed one two three and we're going to go down to the login button we hit login and we're still hitting some type of problem so the data couldn't be read because it is missing so let's go ahead and take a look at what we're we're saying and what I want to do is I want to make sure that my user object is all lined up properly we have the ID I'll see look at that might be the problem right there ID as a string we wanted to change that into an integer because look at what's coming back an integer so I had accidentally had the user have an ID of type string let's try this one more time I think everything else should be cleared up by now and we should be able to actually log into our app so let me do kela loco and our password is passed one two three but let's add a four on there make sure that we're still getting back our error we get back our error now let's try it with the correct passcode which is passed one two three we're gonna try to login and now we actually get into our app so I know that this was an extremely long video but I think it was well worth it we covered essentially everything that you need to do in order to be able to log into your app I think that it was actually beneficial to show you that I was kind of running into errors I had to go back and change a couple of things because I got packed you know a couple things that I didn't expect it's been a while since I had to write a networking layer like from scratch with bare-bones but you know this is kind of how you do it now what we want to do is we want to kind of just make it so that we're able to do the same exact thing but we want to be able be able to send like encoded object right so this will actually be fairly quick now that we have a majority of the functionality built out all we need to do is we need to go ahead and take abstract away the parts that we're gonna actually be using which is everything in here which is going to be our request so let's go ahead and abstract away everything that's that we're doing to handle our requests so all we want to do is we want to make sure that we're gonna pass in a request to a new function called handle response and that's going to essentially take in the requests that we created and it's going to take in a completion handler which is going to be the exact same type of completion Handler here all right so now that we have this handle response what we can do is we can go back down here after we after we're about to pass in the task or after we finish adding the headers to the request and we could just scrap all this code down here and add it into that new function that we created because we're actually going to be using the same functionality for both types of request methods so now all we need to do is we need to handle the make sure we're doing handle response and we're passing it into this request function right here all right so we're able to pass in the request right here and then we're able to pass in the completion because it has the same type of completion signature as up here so we're able to continue and pass that all through and I'm actually gonna put these on separate lines just so that it's a little bit cleaner just like the other one and now what we want to do is we want to create another request that allows us to pass in actual encoded object so for this specific recipt instead of parameters we want to be able to accept a login request or yeah a login object essentially that we'll handle which parameters were supposed to be passing to it which is going to be encode Abul so what we'll do is we're going to copy all this functionality right here we're gonna create a new function that's almost exactly like it and now instead of parameters what we want to do is we want to have a login object and then we're gonna pass in a login request login object we'll just pass in a login and we need to create an object for that alright so now that we have this new object called login which is conforming to the encode Abul protocol and also has the two properties that we're going to be needing to pass into our API which is just simply the username and password what we can do is we can go back over to our networking service this is now going to have a login object and we're no longer going to need to create components right here so what we can do is we can get rid of all this all this code right here that we were using to create components and instead what we want to do is we want to create data directly from that login object that we're passing right here so let's do that now all right so as you can see what we did was we edit this new do catch block and all we're going to do is we're gonna pass since we're passing in this login object we're going to create data from that login object using a JSON encoder and we're just going to encode our login object if we're able to do that then we can just set that data directly to the HTTP body if we aren't able to do that then what we're gonna do is we're gonna have a new error pop up that says bad encoding so let's go ahead and go back over to our view controller and what we're gonna do let's go back not to the main app at the login view controller and instead of having all this information in here the way that we're doing it let's create two new functions so that we could separate the different types of requests that we can do but before I do that I also forgot to do to change the to change the type of content that we're going to be passing so we're no longer passing URL encoded data what we want to do is we want to say application JSON because we're actually going to be passing JSON as the request it's no longer going to be form data so now that we have it set as JSON when we go ahead and go back over to our login view controller and we create those two functions everything should work out pretty smoothly all right so as you can see in the did tap login button what we did was we took all that functionality out of here that's doing the networking request and we're actually just pasting it in here now we created this function right here called form data request just to distinguish which which type of request we're doing depending on what your back-end is accepting some accept both some accept only one or the other so if you're doing a form data request and this is how this is the request that we're gonna want to do but and we're going to just do it just like we did before but if we're gonna be doing a JSON request we need to create a login object and pass that to the new request method that we did in our networking service so let's do that now alright so now that we created the login object from our username and password we're just going to use our new request method which is also taking an endpoint but instead of parameters it's taking in a login object and then everything after that point is going to be the exact same now I'm gonna keep it this way just so that it's easier to see because most likely you're only going to be using one or the other so you can just keep the code the way that it is but you know if we were going to end up keeping it like this what you could do is you could extract all this out into a new function which is just gonna handle the result itself but let's go ahead and remember that we only have the form data requests currently selected so what I want to do is I want to make sure that the form data request is still working and as we expect it to make sure that the error handling is working make sure that the proper login is working and then we'll switch over to the JSON so this is gonna be the form data test first we're gonna try to log in we're still getting the correct error message let's do the correct login credentials all right and we log in so we're saying welcome Kyle looking pretty good pretty good pretty good so now let's try to do the same thing with the JSON request where we're actually encoding the objects into JSON before we send it up all right so we want to try to do a login see if the error is still working still getting the correct error awesome now let's do the correct credential all right so those are the correct foreign credentials and let's try to log in and bam welcome Kyle so that's all we need to know that's everything that we're gonna cover today this was a long enough video if you liked it make sure that you share it with as many people as you possibly can started it was so long but as you can see there's a lot of little steps that go in between but yeah let me know what you guys thought about the video if there's any other topics you want covered leave a comment in the description and you know what just go out there keep coding passionately you
Info
Channel: Kilo Loco
Views: 24,788
Rating: undefined out of 5
Keywords: swift networking, swift post request, swift form data, swift json, swift json request, swift login, swift database, swift database login, swift urlsessions, urlsession post request, urlsession form data, urlsession json, swift urlsession encodable, swift urlsession decodable, swift x-www-form-urlencoded, swift urlcomponents, swift httpbody, swift dictionary to data, swift object to data, swift struct to data, swift encodable to data, swift php, swift javascript, python
Id: XYRykbU97yA
Channel Id: undefined
Length: 48min 12sec (2892 seconds)
Published: Sun Feb 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.