Objectives of the blog
Implement Facebook login using Facebook SDK in iOS app. OR you can download the sample project from this link.
Procedure to implement
1. Create a new Xcode project. And open the project.
2. Open the Terminal and type “cd “. Then drag the project file from Xcode to the terminal. This will add the path of the project folder into the Terminal. Then delete the project name “Sample Facebook Login.xcodeproj” from the command. Final command in Xcode will be like “cd /Users/macpro/Desktop/iOS/Sample Facebook Login/“, this is the path of the project.
3. In terminal, use command “pod init“, this will create a podfile for the project.
4. Use command “nano podfile“, this will open the podfile in editing mode. The add below pods in the file.
pod ‘FBSDKCoreKit’
pod ‘FBSDKShareKit’
pod ‘FBSDKLoginKit’
5. The podfile will look like below screenshot.
6. Use command “control + X“, to exit the file and save the podfile.
7. Run command “pod install” to install the podfile in the project. You will see the process terminal like below:
8. After the installation is complete, close the xcode project. Go to the project folder, you will see new project named “[your project name].wcWorkspace“. Open this project.
9. Open file named “AppDelegate.swift” in your project. Import “FBSDKCoreKit” and add below code in func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)
FBSDKApplicationDelegate.sharedInstance()?.application(application, didFinishLaunchingWithOptions: launchOptions)
The code will look like below:
10. Go to this link to create a new Facebook project – https://developers.facebook.com/apps/
11. Click on “Add a new App“. Enter your projects name and proceed.
12. Fill the details on the next page. Go to “Settings” in left menu, then go to “Basic” and fill the details.
13. Go to bottom of the page. Click on “Add Platform“. Then fill you apps bundle ID and you will see a screen as below:
14. Then go to your Xcode project. Open info.plist file (right click -> Open As -> Source Code), then add below code in your info.plist file.
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array <string>fb[your Facebook App Id that you will get in step 11]</string> </array> </dict> </array> <key>FacebookAppID</key> <string>[your facebook App Id that you will get in step 11]</string> <key>FacebookDisplayName</key> <string>[your facebook App Name]</string>
15. Open your ViewController where you want to add Facebook button for login. Import “FBSDKLoginKit” and add below code in viewDidLoad() function.
// Creating an instance of facebook login button. let loginButton = FBSDKLoginButton() // Setting permission to facebook button. loginButton.readPermissions = ["email", "public_profile"] // Setting delegate of facebook button so that we can access the response we get from facebook after login. loginButton.delegate = self // Centering the facboiok login button and adding the button in main view loginButton.center = self.view.center self.view.addSubview(loginButton)
16. Add Facebook button delegate and the delegate methods.
17. To get the user details you need to add below code in func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result:
FBSDKLoginManagerLoginResult!, error: Error!) delegate method of Facebook button.
// If we get token in response, that means Facebook login is successfull. Below code is used to get user details after facebook login is successfull. if((FBSDKAccessToken.current()) != nil){ FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, result, error) -> Void in if (error == nil){ // Result will contain the user details that can be fetched. print(result ?? "") } }) }
18. The Final code will look like below:
19. Run the project and use the button “Log in”. You will be logged into the Facebook. You will get the user details in “result” variable, from which you can parse the details into your project.
Summary
After following the above procedure, you will be able to implement Facebook authentication in iOS Apps.