我正在开发一个使用Facebook登录和共享的iOS应用程序。这需要在各个地方检查活动会话状态。
Facebook提供的代码检查当前状态并决定是否打开新会话。但是,记录会话状态表明它可能是开放的,即使与FBSessionState常量的比较可能表明它已关闭。
例如,使用以下代码(facebook' s),即使我的会话状态记录为1
,它也不等于FBSessionStateOpen
或FBSessionStateOpenTokenExtended
,并开了一个新的会议。
NSLog(@"facebook session state: %lu", FBSession.activeSession.state);
// log shows 1
if (FBSession.activeSession.state == FBSessionStateOpen
|| FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {
NSLog(@"facebook session already open");
// do stuff...
} else {
// new session opened anyway
NSLog(@"open new facebook session");
/* code to open a session omitted here
...
*/
}
尽管定义不变,但显示1 = FBSessionStateOpen
。
FBSession.h:
typedef NS_ENUM(NSUInteger, FBSessionState) {
/*! One of two initial states indicating that no valid cached token was found */
FBSessionStateCreated = 0,
/*! One of two initial session states indicating that a cached token was loaded;
when a session is in this state, a call to open* will result in an open session,
without UX or app-switching*/
FBSessionStateCreatedTokenLoaded = 1,
/*! One of three pre-open session states indicating that an attempt to open the session
is underway*/
FBSessionStateCreatedOpening = 2,
/*! Open session state indicating user has logged in or a cached token is available */
FBSessionStateOpen = 1 | FB_SESSIONSTATEOPENBIT,
/*! Open session state indicating token has been extended, or the user has granted additional permissions */
FBSessionStateOpenTokenExtended = 2 | FB_SESSIONSTATEOPENBIT,
/*! Closed session state indicating that a login attempt failed */
FBSessionStateClosedLoginFailed = 1 | FB_SESSIONSTATETERMINALBIT, // NSError obj w/more info
/*! Closed session state indicating that the session was closed, but the users token
remains cached on the device for later use */
FBSessionStateClosed = 2 | FB_SESSIONSTATETERMINALBIT, // "
};
这是一个错误还是有什么理由说明为什么facebook的代码应该真正开启一个新的会话?我应该与州的int
值而不是常量进行比较吗?为什么记录的值不同意常量?
...此外,重新打开状态后记录状态,它会记录为513或514,这与任何常量都不一致。
答案 0 :(得分:0)
根据常量的typedef枚举,状态1
为FBSessionStateCreatedTokenLoaded
。评论说"两个初始会话状态之一,表明已加载缓存的令牌;当会话处于此状态时,对open *的调用将导致打开会话,而无需UX或应用程序切换"。
所以我接受了这个建议,解决方案运行良好,会话在没有UX或应用程序切换的情况下打开:
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
NSLog(@"fb session state created token loaded.");
[FBSession.activeSession openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (FBSession.activeSession.state == FBSessionStateOpen) {
// do stuff
}
}];
}
else if (FBSession.activeSession.state == FBSessionStateOpen
|| FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {
NSLog(@"fb session already open");
// do stuff
} else {
NSLog(@"open new facebook session");
// open new session...
}