14

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?

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
hutututu
  • 239
  • 1
  • 4
  • 11

4 Answers4

22

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

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
chedabob
  • 5,835
  • 2
  • 24
  • 44
  • 2
    They did ask for Swift, which would be `FBSDKAccessToken.currentAccessToken()`. – Marcus Adams May 13 '15 at 18:47
  • Thanks for this information. However, I have another doubt that now when started app another time how can I skip the login page to directly go to profile page. I am sure this is something to do with secure but I am a bit confused – hutututu May 13 '15 at 18:49
  • You'd need to call that function in your AppDelegate and choose which ViewController to go to based on that – chedabob May 13 '15 at 18:50
  • Ok, so does this approach of setting view controller from AppDelegate works even when I am using storyboard to build my app. – hutututu May 13 '15 at 18:54
  • Yes but rather than letting the app use the Storyboard specified in your Info.plist, you must instantiate it yourself. See this answer for an example: http://stackoverflow.com/a/26757245/78496 You will have to give your ViewControllers identifiers if you haven't already: http://stackoverflow.com/a/8186721/78496 – chedabob May 13 '15 at 18:58
  • For me it doesn't return nil however when I take that token and validate it this is what I get Facebook::AuthenticationError: type: OAuthException, code: 190, error_subcode: 458, message: Error validating access token: The user has not authorized application – Vinod Sobale Jul 27 '16 at 05:24
  • changed to **AccessToken.current** – janaz Aug 18 '19 at 13:27
6

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()

Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64
Pung Worathiti Manosroi
  • 1,432
  • 1
  • 20
  • 23
  • it should not work. As FBSDKAccessToken.current() is returning non-nil value. check the return type. its FBSDKAccessToken! which guarantees that this could never be nil. I don't know how its working, but its working. – Aaban Tariq Murtaza Mar 22 '17 at 03:59
  • I think following should be the code... if([FBSDKAccessToken currentAccessToken] != nil){ // logged in }else{ // not logged in } – Chirag Purohit Nov 12 '17 at 07:55
3

in sdk for swift 5 use the next portion of code:

import FBSDKCoreKit

if AccessToken.isCurrentAccessTokenActive {
    print("your session is active")
}
RAUL QUISPE
  • 800
  • 9
  • 13
0

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.

onCompletion
  • 6,500
  • 4
  • 28
  • 37