With the introduction of Swift by Apple, all of sudden every aspect of iOS development changes. Today in every apps, login through social networking is used. Facebook also released their SDK with swift language and in this tutorial we will learn how to login using Facebook swift SDK and get logged in user profile information.
Set up app on Facebook developer and add app ID to your project .plist file
Create a new project (I am considering that you know how to create a project using xcode). Now go to https://developers.facebook.com/
On right side corner, you will find “My Apps“, on its expansion click “Add a new app“. Enter the information as per the fields. Select the platform for your app as iOS.
Skip the first and second step as we are going to use swift SDK and that can only be installed using pods. Enter you bundle id of your project (it should be same as listed in your project, otherwise Facebook login will not work). Skip 4th step and copy the .plist info shown in 5th step to your project .plist file (if you don’t know how to do it then right click on your project .plist file and open it as source file, after that paste the info as shown in the Facebook developer portal).
Install Facebook SDK using pods
You can install Facebook SDk using cocoa pods by visiting this link https://github.com/facebook/facebook-sdk-swift
For how to install cocoa-pods, you can learn more about here
Integration of Facebook login with Swift
At this stage we are done with our Swift3 SDK for Facebook. Open your AppDelegate.swift and import FacebookLogin and FacebookCore frameworks. Made changes to your AppDelegate.swift file as shown below
Open you ViewController.swift file, create an IBAction for Facebook login and named it loginWithFacebook. Below is the code that get authentication token from Facebook. We are showing code for the whole file, and please import frameworks as done in AppDelegate.swift.
If all goes well without any error, you will get authentication token from Facebook.
You can use below code to get user basic information, like name, email and Facebook ID.
Where to go from here
In this tutorial, you learned how to login and get profile information from Facebook using Facebook swift3 SDK in iOS. You can download the source code from Facebook-login-with-swift3. If you have any questions, feel free to comment and I will try to answer those as soon as I can. Stay tuned for more tutorials on swift.