开发者

facebook-ios-sdk logout

开发者 https://www.devze.com 2023-01-28 05:46 出处:网络
I am building a basic app for school that gets some info from facebook using the facebook-ios-sdk.However, when I log out of the app, it does not give me the option to log back in, even in the demo fr

I am building a basic app for school that gets some info from facebook using the facebook-ios-sdk. However, when I log out of the app, it does not give me the option to log back in, even in the demo from facebook. I am checking to see if the sessions is still valid, and it always comes out invalid. That is another problem. Here is the code I have. Any help is appreciated.

- (void)viewDidLoad {
      _facebook = [[Facebook alloc] init];

      if ([_facebook isSessionValid] == NO) {
       //show proper buttons for login
        loginButton开发者_运维问答.hidden = NO;
        logoutButton.hidden = YES;
       }

     else {
     //show proper buttons for logout
        loginButton.hidden = YES;
        logoutButton.hidden = NO;
    }
  }

That is to check whether I am logged in or not. Then I have the proper buttons showing, but the code above is always returning that the session is invalid. Here are the functions I call to log in or out:

- (void)login {
     [_facebook authorize:kAppId permissions:_permissions delegate:self];
}

/**
* Invalidate the access token and clear the cookie.
*/
- (void)logout {
     [_facebook logout:self];
}


The facebook object says it has no valid session because its accessToken and/or expirationDate are nil. That's probably because it doesn't persist them itself. You probably need to record the accessToken and expirationDate by handling the login in an FBSessionDelegate's fbDidLogin method.

- (void)fbDidLogin {
    [[NSUserDefaults standardUserDefaults] setValue: _facebook.accessToken forKey: @"access_token"];
    [[NSUserDefaults standardUserDefaults] setValue: _facebook.expirationDate forKey: @"expiration_date"];
}

Use NSUserDefaults, for example, to persist those values to the device. Then, every time you alloc and init your _facebook object, immediately set the accessToken and expirationDate to the values you stored in NSUserDefaults.

That should fix the facebook object saying isSessionValid = NO always. As for the other problem, I have the same one :(

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号