- Pod file
pod 'FBSDKCoreKit'
pod 'FBSDKShareKit'
pod 'FBSDKLoginKit'
- 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
}
- 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")
}
}
}