The Startup
Published in

The Startup

Facebook’s CORRECTED OAuth Documentation

Facebook’s current documentation is out of date…

The Problem: If you follow the steps according to the documentation exactly, your application may work in your simulator, but it may not work on your real device (I’m using an iPhone 11, so I don’t know for sure about other models).

The Solution: Check step 6 if all you want is the solution.

Using:

  • XCode 11.2.1
  • iOS 13.3.1
  • Cocoapods (FBSDKLoginKit)
  • Swift 5

1. Create a new Single View Application

Basic stuff first.

Name it something.

Save it wherever you want.

Go to your terminal and navigate to your project’s folder. Run pod init and pod install to get your project set up with cocoapods.

Now open your project workspace file with XCode.

This should open your project with XCode

Find your Podfile and add your dependencies: FBSDKLoginKit & FBSDKLoginCore

Go back to your terminal and run another pod install to get your dependencies into your project.

2. Create a button that will start the login flow.

Go to your root view controller and add a new custom button.

Make sure you can see it logging your print statement when you press it.

2. Create an app in Facebook’s dashboard.

Go to the following url to build your app with Facebook:

https://developers.facebook.com/docs/facebook-login/ios

I’ll go through each step with you…

  1. Either create a new app or choose an existing one. I’m going to create a new one. Click “Create a New App” , once you’re done it should look like this.

2. Change package manager to Cocoapods

3. For step 3, we’re going to need our bundle identifier so that when we finish the oauth flow, the facebook app knows which application to redirect back to.

You can find your bundle identifier here.

It’s the highlighted text

Paste that into step 3 and hit save.

Also, enable single sign-on for your app.

4. Add config to your info.plist file.

Basically, you just need to copy both of these code chunks and put them into your info.plist, here’s what it should look like in the end…

5. Connect your app delegate (This is where they screwed up)

They give you 2 code blocks in this step, we’ll only need the Swift code.

To make this super clear, I’ll copy the code into an editor to point out the mistake.

Ignore the error, this is just a demonstration of the code that they give you.

They give you 2 functions in your AppDelegate class.

The function that I’ve highlighted contains some setup that you’ll need in your application. Just grab the highlighted section and paste that into your real AppDelegate.

Before
After: (P.S., don’t forget to add the import FBSDKCoreKit statement)

Now go back to the injectable code that they give you. You see the second function there? You don’t actually need it. Here’s what you actually need to do…

Copy this function:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
return
}
let _ = ApplicationDelegate.shared.application(
UIApplication.shared,
open: url,
sourceApplication: nil,
annotation: [UIApplication.OpenURLOptionsKey.annotation])
}

You need to add this function to your SceneDelegate file. So let’s do that.

Before
After: (P.S., again, don’t forget to add import FBSDKCoreKit)

6. Make our Facebook button actually work.

Go back to your ViewController and add a callback to the facebook button.

All we’re doing here is…

  1. Checking to see if our AccessToken class (provided by FBSDKLoginKit) has an access token (meaning we’ve logged in before).
  2. If we have one, then we log out (remove the access token from the app).
  3. If we don’t have one, we take our user through the login flow, and handle a callback when that login flow has finished.

Now everything should be working properly! 🎉

If you learned something today, cheers to you :)

Happy Coding 📈

--

--

--

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +756K followers.

Recommended from Medium

Ruby Basics For JavaScript Monoglots

How To Use Async/Await In Flutter

To Tokenise to not to Tokenise — A Suggested Model

Warehouse62 220v Battery Power Station

5 Great Software and tools for Music Production

C++ For loop

How to access S3 hosted website via CloudFront using OAI(Origin Access Identity)

Daily Scrum — Make it Awesome

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alec Mather

Alec Mather

Developer 💻 || Writer 📝

More from Medium

Continuous Delivery of iOS applications using Fastlane

iOS CI/CD Experiments: GitLab (Runner) + Fastlane + SwiftLint

Store an Array of Custom Data Types in Core Data With Transformable and NSSecureCoding

Handling Concurrency with Async Await in Swift