Facebook Login Quick Start – iOS Swift 3 and Xcode 8

Here is yet another entry in my iOS developer journey in which I try to improve upon available official documentation.

Facebook’s login SDK for iOS has been around for a while and has gone through several iterations/updates. There are various tutorials online which have these issues among others:

  • Official Facebook Login SDK for Swift docs do not go over required Info.plist and AppDelegate.swift changes for their SDK to work, and appears to use old Swift 2 syntax.
  • Various online tutorials are still using no longer necessary objective-C bridging headers.
  • Facebook’s developer quick start for iOS steps only use objective-C syntax.

So as with Parse, I will combine the steps for doing a quick and easy Facebook Login setup in Swift 3.

Quick Start

  1. Create new app at https://developers.facebook.com. This is straight-forward and simply requires a unique display name.
  2. Install Facebook libraries using cocoapods. You can do this using carthage or manually, but so far I’ve found cocoapods to be the most ubiquitous and easy way. If you haven’t used Cocoapods before, my previous post on using Parse has some additional information.
    target 'XCODE-PROJECT-NAME' do
      use_frameworks!
    
      pod 'FacebookCore'
      pod 'FacebookLogin'
      pod 'FacebookShare'
    end
    
  3. Go to your new facebook app’s dashboard on https://developers.facebook.com and use “Quick Start” button under Facebook Login section to set your bundle identifier and to get the required code for your project’s Info.plist file. You can ignore/skip all the steps except 3 and 5. Make sure you get both sections for your Info.plist file in step 5.
  4. Add AppDelegate.swift methods that enable Facebook to leave and return to your app when your users try to sign in with Facebook.
    import FacebookCore
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
            return true
        }
        
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
            let handled = SDKApplicationDelegate.shared.application(app, open: url, options: options)
            return handled
        }
    
  5. Finally add the code for creating a facebook login button in your app.
    import FacebookLogin
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
                let loginButton = LoginButton(readPermissions: [ .publicProfile ])
                loginButton.center = view.center
                view.addSubview(loginButton)
        }
    }

You should now have an access token with public profile permissions for the user account that signed in with the facebook login button.

Notes/Errors

  • You may get the error: “OSStatus error -10814″. Don’t worry, this just means the facebook app isn’t installed in your simulator or on your device. Facebook Login API will still work.
  • [Update September 27, 2017] You might notice 3 errors (“contentDescription” etc) in your xcode project resulting from the facebook cocoapods. There’s an existing issue thread about these due to deprecated code, and it might very well be fixed by the time you’re reading this tutorial. The fix is easy, as you can simply comment out the lines that result in the errors.

9 thoughts on “Facebook Login Quick Start – iOS Swift 3 and Xcode 8

    1. Hey Mac. I just ran through the steps and other than a minor issue due to deprecated code in the facebook cocoapods (see update at the end of the post), they worked perfectly. What didn’t work for you?

      1. HI,

        I get an “no module” error when I put “import Facebooklogin” in my ViewController. It’s a setup issue I’m sure, bu this tutorial doesn’t cover that and it’s vital.

      2. Nope… I get this:

        import FacebookCore No such module ‘FacebookCore’

        This is my podfile output:

        Macintosh:FacebookLoginExample rsenour$ pod install
        Analyzing dependencies
        Downloading dependencies
        Installing Bolts (1.8.4)
        Installing FBSDKCoreKit (4.26.0)
        Installing FBSDKLoginKit (4.26.0)
        Installing FBSDKShareKit (4.26.0)
        Installing FacebookCore (0.2.0)
        Installing FacebookLogin (0.2.0)
        Installing FacebookShare (0.2.0)
        Generating Pods project
        Integrating client project

        [!] Please close any current Xcode sessions and use `FacebookLoginExample.xcworkspace` for this project from now on.
        Sending stats
        Pod installation complete! There are 3 dependencies from the Podfile and 7 total pods installed.

      3. You’re definitely opening the newly created .xcworkspace file correct? I’ve ran into the “no such module” issue once before and was able to fix it by performing clean folder function:
        Press Command+Option+Shift+K and then Run your app.
        Or from the menu -> Product, press Option on your keyboard and you’ll see Clean Build Folder.

        Once it only worked after restarting the computer. Xcode is just weird sometimes :. Let me know if that fixes it, I’ll add it to this post as well.

  1. OK, it works now… resetting helped and there was some code that had to be modified. I realized only tonight that I’m using Xcode 9. Sheesh. Thanks for your patience.

Leave a reply to mac senour Cancel reply