Skip to content

Instantly share code, notes, and snippets.

@leonmak
Created August 10, 2017 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonmak/a5d20c12326b7fe24a926b2646f8d498 to your computer and use it in GitHub Desktop.
Save leonmak/a5d20c12326b7fe24a926b2646f8d498 to your computer and use it in GitHub Desktop.
FB Login on iOS - Swift 3
  1. Pod file
pod 'FBSDKCoreKit'
pod 'FBSDKShareKit'
pod 'FBSDKLoginKit'
  1. AppDelegate
import FBSDKCoreKit
...
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // FB Login
        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
        
        return true
    }

    // Facebook login
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        
        let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
        
        return handled
    }
  1. ViewController
import FBSDKLoginKit

class ViewController: UIViewController, FBSDKLoginButtonDelegate {
    func setupFacebookLoginBtn() {
        let loginButton = FBSDKLoginButton()
        view.addSubview(loginButton)
        //frame's are obselete, please use constraints instead because its 2016 after all
        loginButton.frame = CGRect(x: 16, y: 50, width: view.frame.width - 32, height: 50)
        loginButton.delegate = self
    }

    
    override func viewDidAppear(_ animated: Bool) {
        let token = FBSDKAccessToken.current()
        if token != nil {
            print("Logged in already")
            print("token: ", token!.tokenString)
        } else {
            print("Not logged in")
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment