Monday, January 24, 2011

How to Do Autologin to Facebook Using FBConnect

Trying to integrate Facebook connection into a non-disclosured iPhone application, suffering with lack of documentation. Wonder why everything has to be done the hard way... for example auto-login was missing from demo.

Grab FBConnect sources from FBConnect Github repository, fix the demo to work for you (instrunction how to do that were long, but excellent) - and then add these modifications in DemoAppViewController.m:

- (void)fbDidLogin
{
    [[NSUserDefaults standardUserDefaults] setObject:self.facebook.accessToken forKey:@"FBAccessToken"];
    [[NSUserDefaults standardUserDefaults] setObject:self.facebook.expirationDate forKey:@"FBExpirationDate"];
- (void)viewDidLoad
{
    self.facebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"FBAccessToken"];
    self.facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:@"FBExpirationDate"];
    if ([self.facebook isSessionValid] == NO)
        [self.facebook authorize:_permissions delegate:self];
    else
        [self fbDidLogin];
- (void)fbDidLogout
{
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"FBAccessToken"];
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"FBExpirationDate"];
}
Remember this is NOT all the code you need, just what was needed to add in my case. Your FBConnect demo application might differ. Also note that I had turned "Single Sing-On" off, no idea how this code would work when it's enabled. Will worry about that later, if needed by the "real application".

No comments:

Post a Comment