iOS Swift: How to integrate the Facebook Login SDK and getting user s email address

swiftgiftfacebook This is a topic that many people are looking for. savegooglewave.com is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, savegooglewave.com would like to introduce to you iOS Swift: How to integrate the Facebook Login SDK and getting user s email address. Following along are instructions in the video below:

“What is up guys. Today s tutorial is going to talk about facebook. Login and and how to integrate the sdk inside of our ios application. So that users can log in with facebook.

Instead of inputting. Their own email address and password. So this project. Here that you re seeing on the screen is called touch of modern.

I worked on this project all of last year. And if you can probably download it from the app store. If you really want to take a look at how it works. The main login page looks like this you can enter your user information or password.

If you want or you can login with the facebook login button. If i m hitting okay it authorizes and then it allows you to access the app. Now you re probably here. Because you also want to learn how to integrate facebook.

And it s not that difficult actually so i ll show you how to create an application that looks somewhat like this and if we have enough time you will get into how to actually get the friends of your facebook account as well so without further ado let s start by going into xcode and command shift n. Single view application. I m going to call it youtube demo for doesn t matter too much what you call it create there we go and let s run that on the iphone 6s it gets it s a rather smaller simulator easier to work with command 3 to shrink. It down just a little bit and now we have our application set up and our project is ready to go so next question is what do we do in order to get facebook to work we first go to developersgooglecom.

We will now google search facebook ios sdk the first link clicking into that we see a couple of different okay so i m going to go into the getting started page and then we ll go through each each one of these steps. Omitting probably step. 7. And it s not going to take that long actually should go by quite fast okay step.

1. Is download the sdk set that and then wait for that guide to download. It s quite big in size 48. Megabytes and next.

We want to create our facebook application by just hitting that button and then creating youtube demo like for vta doesn t matter exactly what you call it leave that optional field blank education. This is not a test app. So we create the app. And it says to verify by clicking the wristwatches there we go finding a submit facebook is going to create the server app or the app on their servers.

And then now we have app id and a bunch of other information about our new facebook application next. We go into a settings. But before we do that we move on to step..

.


3 go to settings settings hit add platform and choose ios app platform choose ios and then now. We do provide your bundle identifier. As the bundle id field. Which is that so we get that by choosing the bundle identifier hitting command a to in select all command c command v.

To copy and paste and then it tells us to enable a single sign on enable save changes cool that is step. 3 moving on to step. 4. We re going to want to open up the actual download of the sdk and it s that right there if i go into finder go to downloads.

And i already have this sdk downloaded and extract it on to my downloads directory. And what it s telling us to do is to import these two framework files into our project by simply dragging it from the finder to the xcode project. Like this click finish. And so not too hard right pretty simple.

If you ask me so let s go ahead and move on to step. 5. Just ignore the rest of these other steps. Because you don t really need to integrate that anymore.

And here s the somewhat tricky your part well not too. Tricky but let s open. Up the. Infoplist open up as source code to do that s what they re telling us.

And we just want to copy and paste this into the source under dict because this entire thing is the dictionary. And we need to replace these app. Ids with our app id in order to get the app id go back to the dashboard. Replace your app id.

By that your app id here and then whatever you called your application in our case. It is youtube demo for i believe and hit save. I think we re good next for ios. 9.

Which is the current version that we re using. If you re watching this a year from now and ios this 10 is out i guarantee you that there s going to be additional steps to avoid the security protocols and we do that ok so essentially that s allowing us to hit the facebookcom servers. Okay alright step six final step and this is the fun and interesting part of actually writing out code and integrating the sdk inside of our project. We do that by entering these two methods inside of our app.

Delegate and you notice that the this piece of code is in objective c. And we want to write and slip. Because swift is just so much easier to write and to maintain now in order to actually use those import statements..

.


We want to create a bridging header file and we do that very simply by creating a objective c file call it anything you want to create and hit this create bridging header option that allows us to now modify this header file with this statement right here and then if we want to delete that we can doesn t really matter. We can run the application now everything is okay next we implement the methods on fb sdk application delegate. Quite the mouthful but not too hard to type out if we do at the sdk application delegate. You know shared an instance application.

You will use did finish with lot options options and next. It wants us to do open url like that so we will do facebook application delegate share instance application open url application url source application tation and we re going to want to return the entire value all right let s get the implementation of the logging in inside of the view controller now okay. So what do we do here and in order to figure that out we will go back to the facebook. Getting started page.

And we ll go into setting up this application here so in this specific step of step 5. I ll click into preparing your app for ios 9. Which is also a left bar right here and you want to set up this last chunk for version 60. Of the sdk.

Which is what we re using here and go back to the p list to include that query scheme that will allow us to authenticate with the facebook servers. So now that we have that done i look back to getting started and hit this add a facebook login button to figure out what exactly we need so going to step. 3 here i noticed we need to import this login kit framework as well as the core kit to do that we go back to the header. The bridging header and just copy and paste that in there hit build see if it works everything looks ok.

Now we want to include the code that adds adds. The login button on to the view controller and we do that by specifying a couple of things here so let s create the login button ui fps dko login one so i m going to create it here and we can return the button like that okay and in order to get the email from the user we need to specify three permissions in a array like that now we will add that login button as a sub view next. We specify buttons a center to quickly get it onto the screen like this running of that will see the login button right in the center. There and it s telling us to log in so let s go ahead and click into that and then continue as whoever you are now i m actually already logged in so you re going to be prompted with a message to log in as your account and upon doing so we see that the message on the button.

Now it says a login button that means we ve successfully successfully i ll dedicate ourselves with facebook and now we just need to get the actual email address of your user. So there s one more thing before we implement that piece of code and we can figure out when we ve logged in by using a delegate method. And that is a you take a look at what that delegate is it s a log and elegant like that and we need three methods to be implemented in order to set ourselves as that delegate. So i hit login get the first one and if i don t implement these methods you see that warning says that we haven t implemented at all.

I ll do that login that just returned true so complete it login run that guy one more time and first. I m going to log out then i log back in and you see that it actually hit line 28. Upon successfully completing the login process next. We want to check upon across sessions the application actually remembers that you re logged in and we can check for that by checking on the current access token like that and then we can call fetch profile.

Which is a new method. We will create right below it and we need to fetch the actual profile. So let me just do that then so let s also call it here upon successful login and as you can see the application remembers that you were logged in by accessing. The token and calling fetch profile.

Which is printing out a statement. Here inside the logger now the next question is how do we get the email of that user i ll show you by using a graph request. Which is what facebook likes would call it so facebook graph..

.


Requests let s use the top one and me is the path while the parameters will be a dictionary with a key of fields and here you can specify a an array of options. So i m going to use email first name and last name you can also specify the picture now a picture like that works as well. But it returns a really really small low res image. So you want to return a larger image by using type large makes sense right if the you wanna call.

Start with completion handler hit enter to complete that call this connection. This should be result here o. If error is not nil. We print out there okay as a good ios programmer.

You want to make sure you check for the error. And exit automatically outside of the block. When when there is an error. Okay.

Now that we have the proper handle on result. We will access the email by unwrapping it like this and we ll just print out the email into the console run that guy and we will see that my email is being fetched right there into the console. And upon doing that you ve retrieved the user s email information. So you can do with that whatever you want for example log them in to your database make sure that you link every action that they perform to that email address and then later on you can you can email them with whatever you want to email them with and that s that s pretty much the gist of this entire tutorial and let me show you one more thing.

Which is the picture. So if i want to get the actual image. I will have to get it from the result. If i print out a result here you can see that the image resides inside of the picture.

The picture value. Which is picture key data. Key url key. And this is a series of dictionaries.

And the easiest way to unwrap. A data structure like this and it actually took me a while to figure out how to do this safely. Which is by letting picture equal result picture right and that that itself will unwrap our picture object. But it doesn t know what type of object.

It is and we know for the for the most part. We know that it is a dictionary and so picture is the dictionary now and in order to access data. We will let data equal picture data. It s also a dictionary like so and for the url.

We will unwrap it again like this data url and we know that it s not a dictionary. But instead a string having done all that we now have a handle on url to do with it whatever we want to and i m just going to print. It out like that and show you in the chrome browser..

.


What the url will show us well open up new tab enter. And that s me that s my picture. I took recently with my dslr camera that i m actually videotaping with here okay and lastly. We will i guess some of the other options that you can use is to access the public profile or user.

Friends. Now. I don t think user friends works. Anymore.

And you actually have to specify some other some other permission now in my completed project. I was able to access a taggable friends graph path and that allowed me to get the friends for myself attackable friends is not exactly all your friends. If a subset of that list so if you re able to access this list you can construct an array of of friends for yourself. And then you can get their their picture and their name and if you go entirely through the process of constructing a list or a ui view.

A ui collection view controller you can construct this list that you see right here. And that s yeah. That s pretty much the entire tutorial here. Now it probably seemed like a lot in the beginning.

But creating an application in facebook. And then integrating the sdk is easier than you would expect at least in my mind. It was easier than i thought there their documentation. Includes a step on how to set this up with cocoapods.

Which is where is it ok. I believe it s in their advanced section advanced configuration allows you to use cocoapods and install these three sdk frameworks. But i ve tried that and i wasn t able to get my project to compile and use those those sdks so downloading isn t too bad. But make sure to give this a try whenever they update update.

Their documentation on how to exactly do it cool now now that you know how to allow your users to easily piggyback on facebook s login mechanisms you can include that in your application and fetch the email address of your users to do whatever you please okay okay. I hope you enjoy that and make sure to either like the video thumbs up or leave. A comment. Asks any questions if i went over anything too fast.

And yeah or you know hit subscribe always helps out the channel to get more subscribers so enjoy. ” ..

Thank you for watching all the articles on the topic iOS Swift: How to integrate the Facebook Login SDK and getting user s email address. All shares of savegooglewave.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.
description:

tags:

Leave a Comment