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
- Create new app at https://developers.facebook.com. This is straight-forward and simply requires a unique display name.
- 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
- 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.
- 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 }
- 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.