This post has all the steps that iPhone app development company professionals follow to integrate Facebook log-in using Swift. You can read this post and discover what steps are involved and how the team of experts do it.
One-liner: In this tutorial, we will learn how to integrate Facebook Log-in using Swift.
Features: You can log-in directly in the application and fetch user details.
Starts with creating one single view application with the product name “SwiftFacebookIntegration”. In the language, field select Swift and in device field select iPhone.
Before we start coding, we require to create and register Application on Facebook.
Now, to create an application on Facebook follows the below steps:
- Go to https://developers.facebook.com and login with your developer account or sign for one.
- Click on “My Apps” and select “Add a New App”.
- Enter your app name, Contact email and category of your app.
- Go to dashboard screen and select setting -> Add Platform ->
- Copy Bundle ID from Xcode project and paste in Bundle Id field.
- Click to enable to “Single Sign On”.
- Click “Save Changes”.
Download Facebook iOS SDK from here. After download adds FBSDKLoginKit.framework and FBSDKCoreKit.framework in your project.
In info.plist file we have to set returning URL or redirect URL and add Facebook ID. Make required changes as shown in below screenshot.
Open AppDelegate.swift and import FBSDKCoreKit and FBSDKLoginKit frameworks. For configuring Facebook Login with application implement code in didFinishLaunchingWithOptions() like below screenshot. Next, add method openURL() for managing flow once user login and return to the application. For more info, please check below screenshot.
Open Main.storyboard and add one UIImageView in the main view for display user profile picture. Ctrl-drag from image to class and set outlet “facebookImage”. Add one UIView like button size and set class FBSDKLoginButton in identity inspector. Ctrl-drag from the button to class and outlet “btnFacebook”. Add two UILabel with outlet “facebookName” and “facebookEmail” respectively. Please check below screenshots for more info.
Open ViewController.swift file and import FBSDKCoreKit and FBSDKLoginKit frameworks. Next set delegate “FBSDKLoginButtonDelegate”. In viewDidLoad() method add read permissions and delegate. Check below screenshot for more info.
Next two methods are delegate methods of FBSDKLoginButtonDelegate. In the first method if user login successfully we can fetch user info like name, email and profile picture. The second method is for logout. Please check below screenshot.
Now build and run project. If everything is correct, your app should run properly with below output.
iPhone app development company, experts have shared their experience of swift facebook login integration. If there is anything unclear to you, let them know via your comments.