Search code examples
objective-ciosfacebook-ios-sdk

Log in structure for iOS app


Could somebody inform me if this is a proper way to implement a user session for an app:

If I have an iOS app where I want to provide certain functionality only to users who have logged in, and some other kind of functionality to users who haven't (say read vs. write functionality), then does the following architecture make sense...

1) I want to create global BOOL variable, e.g. loggedIn, that I can check against any time an app user wants to perform one of the functions under the "Authenticated-User-Only" list. I would like to know where to store this BOOL. Is NSUserDefaults best practice for storing a global boolean?

2) I'm trying to implement the Facebook iOS SDK, so when I return from a SSO with FB and the FB user has verified my app, can I then use that to set my global BOOL loggedIn to then be used throughout the app?

Thank you in advance.

Note -

This is all client-side, natively within the app. There are no webviews happening, the registry is going to query a server to return whether the user is valid or not.


Solution

  • You could use a BOOL that's global, however that's more work then you'll have to do.

    Instead, you can use the isSessionValid method that is provided by the Facebook iOS SDK to check if the user's session is valid.

    To do this you can simply do the following in any classes that need this check to be performed:

    YOURAPPDELEGATE *delegate = (YOURAPPDELEGATE*)[[UIApplication sharedApplication] delegate]; 
    
    if ([appDelegate.facebook isSessionValid]) {  //Session is valid, facebook is the name of the instance of Facebook that is set in your app delegate, providing that's where you do the login handling, as per the tutorial on Facebook's developer site 
        DO STUFF
    }
    else { //Session is NOT valid
        DO OTHER STUFF
    }