acebook is a great social media and allows user to upload images, videos and links to Facebook platform in form of News Feed and Stories. Nowadays, Facebook is a necessity in most people’s life.

Facebook provides an easy to use SDK to allow app developers to implement the login and logout function inside their apps. After user authentication is verified, app can query the user data on behalf of the user or even share media to Facebook. The data can include user public profile, email and even friend list.

Facebook user data is an important key to let you app know more about user’s information and preferences. It can also help engaging user’s friends to install your app. Do you want to grow up your app by implementing the Facebook SDK? Let’s start here!

Facebook Login Setup

Step 1) Register a Facebook Developer account

You can register a Facebook Developer account at the Facebook Developer Console and create a Facebook project.

Step 2) Add Facebook Login SDK dependencies at iOS project

Facebook Login SDK can be imported by 3 ways, Swift Package Manager, CocoaPods and Carthage. In my opinion, CocoaPods is the most convenient one.

Add the following line of code to Podfile and and run pod install to download the dependency.

pod 'FBSDKLoginKit'

Step 3) Submit the Bundle ID to Facebook

Bundle Identifier is the unique identifier of your app.

Step 4) Configure info.plist

App the following to the info.plist file to set the keys for communication with Facebook server.

Replace those {facebook_app_id} (at line 6 & 11) with your own app id:

Step 5) Final — Update AppDelegate

Login with SDK login button

Facebook SDK provides an easy to use login & logout button — FBSDKLoginButton. By declaring the type of button at Storyboard and the requested permissions (line 9), it is ready to use. The LoginButtonDelegate is triggered when user logs in / out successfully or unsuccessfully.

All apps requesting user’s information other than public_profile and email will be reviewed by Facebook. Facebook would reject any app that cannot provide good reasons for extra sensitive information.

Therefore, please think twice before setting the permission at line 9.

Login with Custom button

Facebook SDK provides a LoginManager to handle login / logout feature and developer can then use a custom login button. Instead of LoginButtonDelegate, LoginManager handles the Facebook login result as a callback.

Facebook Login As as a Tester

Facebook limits the group of public user who can test the login function during app development stage. The above error message is frustrating and the error can be solved by added the tester’s Facebook account as a registered tester at Facebook Console. See my another Medium article about solving this issue:

Facebook SDK sometimes returns unclear error code and error messages. One of them is 1675030 — query error (user is NOT…

Retrieving sensitive user’s information

After user logs in and you get the access token, app can now retrieve user’s personal information from Facebook server. GraphAPI has to be used. You can find the full list of possible fields used in the GraphRequest from the official documentation site:

Graph API User - Documentation - Facebook for Developers

POST /v8.0/ /users HTTP/1.1 Host: graph.facebook.com…

developers.facebook.com

Moreover, according to the official documentation, if your app asks for more than than public_profile and email, it will require review by Facebook before your app can be used by people other than the app's developers. This process probably lasts for several weeks.

Facebook Logout

Facebook SDK logout function is straight forward and loginButtonDidLogOut: loginButton of LoginButtonDelegate is called if default FBSDKLoginButton is used.

Check if user has logged in

AccessToken is a class which stores the logged-in user information. The tokenString is a key to make requests to Facebook APIs on behalf of an app rather than a user.

Facebook Login Summary

  1. Facebook SDK provides authentication feature for retrieving user sensitive personal information after logging in through SDK.

  2. Developer can use either SDK default login button or a custom button to display a login button for user. SDK default one is relatively easier to be implemented.

  3. Access token is generated after user logs in through SDK. It can be used to retrieve user’s personal information from Facebook server.

  4. Sensitive information other than public_profile and email should be requested with good consideration. Facebook review has to be passed before app can be released at App Store and Google Play Store. It can last for several weeks!

  5. Only registered tester can test the Facebook login feature in development stage. Tester’s identifier has to be added at the Facebook Console by admin first.