2

I would like to know what is the best way to bypass the login viewcontroller if the user is logged in already. I have integrated Facebook in my app. I tried to check for the FBSDKAccessToken.currentAccessToken if it is nil or not in the appdelegate class so the app could start either from the loginviewcontroller or not, but it is not working. This is what I tried so far.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


    if ((FBSDKAccessToken.currentAccessToken()) != nil) {
        print("it is logged in")
    }else{
        print("it is not ")
    }
    // Override point for customization after application launch.
    FBSDKLoginButton.classForCoder()



    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

}

Either the user is logged in or not I get the same message. What am I doing wrong? Thank you.

Rafail K.
  • 365
  • 3
  • 14

3 Answers3

1

You cannot check if the user is logged in device facebook app. You can only check if there is a valid access token or not. If there is one, you don't have to prompt user to login again.Because if accessToken is valid, you can get the user info . Again the currentAccessToken has nothing to do whether you are logged in your phone/computer facebook app or not.

You should always depend on the currentAccessToken.

if ((FBSDKAccessToken.currentAccessToken()) != nil) {
        print("user is logged in")
    }else{
        print("user is not ")
    }

If the current access token is nil which is expected by default, you need to initiate the login flow through your app. Please refer to the documentation. Once you have finished the login flow and come back to the app, you will then have a valid access token.

In your case, the access token is nil because you are calling it before the FBSDKApplicationDelegate properly set.

You should do the access token check only after the call to didFinishLaunchingWithOptions is finished. This reason explained in here.

Community
  • 1
  • 1
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • Then how can I check if a user is logged in? By checking if there is an access token? – Rafail K. Aug 29 '16 at 16:20
  • yes. you should always check if there is a valid access token or not. – Teja Nandamuri Aug 29 '16 at 16:21
  • That is exactly what I did in my AppDelegate file. But it is not working. Even if the user is logged in or logged out I get the same message. – Rafail K. Aug 29 '16 at 16:22
  • You should explain what is not working. You will get the valid acces token, only if you initiate the login flow through your app. – Teja Nandamuri Aug 29 '16 at 16:23
  • When I tap the login button I normally log in with my facebook data and successfully I get back the response from facebook in my "LoginViewController". At this "LoginViewController I use the code that is mentioned for FBSDKAccessToken and it is properly working. But when I try to do the same thing in the Appdelegate file I get nothing. For example if I close the app and open it again without log out I should get the message print("user is logged in"). But I always get the other, even if I am logged in or out – Rafail K. Aug 29 '16 at 16:27
  • 1
    It is because you are calling it at very beginning. You should only call it after the FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) is called. – Teja Nandamuri Aug 29 '16 at 16:30
  • That was it. Thank you very much. – Rafail K. Aug 29 '16 at 16:35
1

NOTE: I don't know how to solve the issue in your code, but there is another way of doing this, which I consider better!


LOGIN:

SWIFT 2:

You can add this code to the function that does things if the user login attempt was successful:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("loggedin", forKey: "yourKey")

And then, in AppDelegate.swift add this code, inside ApplicationDidFinishLaunchingWithOptions:

let defaults = NSUserDefaults.standardUserDefaults()
if defaults.valueForKey("yourKey") != nil{
let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
    self.window.rootViewController = viewController!
}

LOGOUT:

In the logout function, add this:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("loggedout", forKey: "yourKey")

And change the if-statement in didFinishLaunching... to this:

  if defaults.valueForKey("yourKey") != nil{

        let vc = UIViewController()
        let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
        let value = defaults.valueForKey("yourKey") as! String!
    if value == "loggedin"{
        vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController!
    }
    else if value == "loggedout"{
        vc = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as! UIViewController!
    }
        self.window.rootViewController = viewController!

}

Hope This Helps!

Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
  • Thank you for your reply. Yes, it helps a lot. But, is this a proper/secure way to solve it? – Rafail K. Aug 29 '16 at 16:18
  • 1
    This is not a proper way to do it, if the acces token is expired, then based on this solution, it still says that user is logged in!! – Teja Nandamuri Aug 29 '16 at 16:19
  • What could be a more proper way to do it? @TejaNandamuri Thank you – Rafail K. Aug 29 '16 at 16:21
  • @user3882720 I think there is another way to properly do this, but unfortunately I cannot tell it to you, because I don't know how! I leave this job to more experienced programmers... Still, you're welcome! – Mr. Xcoder Aug 29 '16 at 16:23
  • @user3882720 and also I've got a question! Does your app provide logout, 'cause if so, I really need to update my answer!... – Mr. Xcoder Aug 29 '16 at 16:24
  • Yes I provide a logout button. Thank you for your answer. – Rafail K. Aug 29 '16 at 16:28
1

I solved it by moving the following code

if ((FBSDKAccessToken.currentAccessToken()) != nil) {
    print("user is logged in")
}else{
    print("user is not ")
}

to my

    func applicationDidBecomeActive(application: UIApplication) {}

Thank you for your replies.

Rafail K.
  • 365
  • 3
  • 14