Reputation: 249
I have started working on an iOS app on Xcode using Swift and storyboards. I have a requirement where on click of a button I will show a user's Facebook profile info on the screen (only if user is signed in using Facebook), but if he is not signed in it will automatically go to login screen and on successful login again the profile view will be shown.
How can I do this?
Upvotes: 14
Views: 15134
Reputation: 799
in sdk for swift 5 use the next portion of code:
import FBSDKCoreKit
if AccessToken.isCurrentAccessTokenActive {
print("your session is active")
}
Upvotes: 3
Reputation: 1490
With latest Facebook SDK, You can check like this :
if FBSDKAccessToken.current() != nil {
// logged in
}
else {
// not logged in
}
They recently changed from FBSDKAccessToken.currentAccessToken()
to FBSDKAccessToken.current()
Upvotes: 6
Reputation: 6650
For more convenient and standard code in swift you can use this following code snippet. 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 specific viewController during app launch based on login check you can place this code inside your project's AppDelegate
and check inside -
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Check here for login
}
Thank You.
Upvotes: 0
Reputation: 5881
If you're using the Facebook Login SDK, call
FBSDKAccessToken.currentAccessToken()
It will be not nil if the user is logged in.
https://developers.facebook.com/docs/facebook-login/ios/v2.3#token
Upvotes: 22