Storing Facebooks access token in NSUserDefaults

Discussion in 'iOS Programming' started by idelovski, Aug 18, 2011.

  1. idelovski, Aug 18, 2011
    Last edited: Aug 18, 2011

    idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #1
    What would be the proper way to handle OAuth access token between two sessions?

    In Facebook's SDK example they don't even bother. The documentation says that if user has official Facebook application on his device and if device is capable of multitasking then the "magic" in Facebook application will take care of everything.

    But what if user doesn't have or even doesn't want the Facebook app on his device? Should I save access token myself in NSUserDefaults? I found example in Facebook Application Development For Dummies, but they store only facebook.accessToken so the thing wasn't working properly at first. Then I played with the code for a while. After looking inside a few of Facebook SDK methods I decided to store facebook.expirationDate too and now the code seem to work.

    But googling around didn't confirm it as a smart long term solution so I'm asking here if someone has more experience with Facebook development. Should I store accessToken/expirationDate or not?

    EDIT - Here's the code I have now:

    Code:
    //Called when the user successfully logged in.
    - (void)fbDidLogin
    {
       NSLog (@"did login");
       
       [[NSUserDefaults standardUserDefaults] setObject:facebook.accessToken
                                                 forKey:kFacebookAccessToken];
       [[NSUserDefaults standardUserDefaults] setObject:facebook.expirationDate
                                                 forKey:kFacebookExpirationDate];
    	
       ...
    }
    
    //Called when the user logged out.
    - (void)fbDidLogout
    {
       NSLog (@"did logout");
    	
       [[NSUserDefaults standardUserDefaults] removeObjectForKey:kFacebookAccessToken];
       [[NSUserDefaults standardUserDefaults] removeObjectForKey:kFacebookExpirationDate];
    
       ...
    }
    And, here is the method from the book, bold lines added by me:

    Code:
    - (NSString *)facebookAccessToken
    {
       if (facebook.accessToken)
          return (facebook.accessToken);
       
       NSString  *savedToken = [[NSUserDefaults standardUserDefaults] objectForKey:kFacebookAccessToken];
       
       if (savedToken)  {
          facebook.accessToken = savedToken;
          [B]facebook.expirationDate = [[NSUserDefaults standardUserDefaults] objectForKey:kFacebookExpirationDate];
          if (![facebook isSessionValid])
             facebook.accessToken = nil;[/B]
       }
       
       return (facebook.accessToken);
    }
     
  2. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #2
    That looks right to me. And yes, if you are requesting "offline_access" permission then you need to store the access token. otherwise it doesn't matter as the user would have to sign in every time anyway.

    depending on what you want to do you may be interested in ShareKit. It's a drop in for multiple social networks.
     
  3. idelovski thread starter macrumors regular

    Joined:
    Sep 11, 2008
    #3

Share This Page