IOS: check if a user is registered using Facebook?

I started working on an iOS application on Xcode using Swift and storyboards. I have a requirement when, when I click the button, I will show the user’s Facebook profile information on the screen (only if the user is signed in using Facebook), but if he is not signed in, he will automatically go to the login screen and will be shown again when he successfully logs in profile view.

How can i do this?

+12
ios swift facebook-login
source share
4 answers

If you use the SDK to log in to Facebook, call

FBSDKAccessToken.currentAccessToken() 

This will not be zero if the user is logged in.

https://developers.facebook.com/docs/facebook-login/ios/v2.3#token

+22
source share

With the latest version of the Facebook SDK, you can check this as follows:

 if FBSDKAccessToken.current() != nil { // logged in } else { // not logged in } 

They recently changed from FBSDKAccessToken.currentAccessToken() to FBSDKAccessToken.current()

+6
source share

For a more convenient and standard code in swift, you can use this next piece of code. BTW I am using Swift 2.2 and Xcode 7.3.1.

 if let loggedInUsingFBTokenCheck = FBSDKAccessToken.currentAccessToken(){ //User is already logged-in. Please do your additional code/task. }else{ //User is not logged-in. Allow the user for login using FB. } 

If you want to load a specific viewController during application startup based on login verification, you can put this code in your AppDelegate project and check inside -

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { //Check here for login } 

Thanks.

0
source share

in the Swift 5 SDK, use the following code:

 import FBSDKCoreKit if AccessToken.isCurrentAccessTokenActive { print("your session is active") } 
0
source share

All Articles