简体   繁体   中英

How to check and renew the state of a Facebook token on iOS SDK 3.0?

I have an iPhone app with an optional Facebook login that the user can activate in the settings.

I don't understand (without using an "proprietary" switch variable to store into the app storage) how to know if the user has activated it in the past, to show its status with a switch.

FBSession.activeSession.state doesn't give a reliable value because the session could be "valid" for the app, but invalid for Facebook, if the token has expired.

In addiction of this, I can't understand how to renew the token when it expires (maybe after a couple of hours?) and without begging the user with a new auth request with the switch to the Facebook app or Safari. Basically, I'd like to keep and use the token until it is valid, and renew it as soon as Facebook reject a request because it is expired.

Thank you in advance!

You can make the following example call:

BOOL openedWithCachedToken = [FBSession openActiveSessionWithAllowLoginUI:NO];

This will not present a login UI to the user if there is no cached token:

See also: https://developers.facebook.com/docs/reference/ios/3.1/class/FBSession#openActiveSessionWithAllowLoginUI%3A

Once you've a cached token (openedWithCachedToken is true) you can make a request (eg requestForMe) to validate the session is valid on Facebook. The Facebook SDK will send a piggy back request on top of yours if necessary, to renew the session. Regardless you can check your own request completion handler to take of the fail scenarios, which may be an invalid server session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM