简体   繁体   中英

How to check if user is logged in with FBSDKLoginKit in iOS?

When I run the app in viewDidLoad I need to check if user was logged in before or not.

if FBSDKAccessToken.currentAccessToken() != nil {
    print("1")
    presentViewController(controller, animated: true, completion: nil)
} else {
    print("2")
    let button = FBSDKLoginButton()
    button.delegate = self
    button.readPermissions = ["public_profile", "user_friends"]
    view.addSubview(button)
}

It always prints "2" . Why?

Below two methods must be implemented in AppDelegate :

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM