Search code examples
iosfacebookfacebook-graph-apifacebook-ios-sdkfacebook-access-token

Storing accessToken in Facebook iOS SDK


I have the Facebook iOS SDK set up in my app. However, I have trouble determining when my session is finished. How to check if it's finished, and where (how) to store the access token received by the login?

I need to determine whether I have the access token or not right from the beginning so I know whether to log in again, or go forward in the app.


Solution

  • Are you familiar with NSUserDefaults ? It is for storing preferences for your app.

    They are extremely easy to use, and probably what you are looking for. So it's just something like ..

    NSUserDefaults *factoids;
    NSString *whateverIDstring; // make this a property for convenience  
    
    factoids = [NSUserDefaults standardUserDefaults];
    self.whateverIDstring = [factoids stringForKey:@"storeTheStringHere"];
    
    if ( ( whateverIDstring == Nil ) || ( [whateverIDstring isEqualToString:@""] ) )
        // it does not yet exist, so try to make a new one...
    else
        // we already made one last time the program ran
    
    // when you make a new one, save it in the prefs file like this...   
    [factoids setObject:yourNewString forKey:@"storeTheStringHere"];
    [factoids synchronize];
    

    Hope it helps!

    ONE get the preferences in to 'factoids' as in the example above

    TWO decide on a name for your preference. 'storeTheStringHere' in the example.

    THREE get your string 'whateverIDstring' in the example using stringForKey:

    FOUR check if it is either nil or blank. if so, start fresh.

    FIVE once you get the value, save it as shown!

    Hope it helps!