好的,所以我现在用超时+小东西解决了这个问题。我真的不喜欢超时,但这是我目前为“未知”会话状态找到的唯一解决方案。这是我的代码:
// This event will rise when FB.init() completed, ONLY if the user is connected
var statusChangeRaised = false;
var statusChangeHandler = function(session) {
statusChangeRaised = true;
console.log('auth.statusChange event callback. session: ' + session.status);
// We only want this handler one time
FB.Event.unsubscribe('auth.statusChange', statusChangeHandler);
handleLoginStatus(d, session, true);
};
FB.Event.subscribe('auth.statusChange', statusChangeHandler);
// Phonegap native plugin
FB.init({ appId : _FB_APP_ID,
nativeInterface : CDV.FB,
status: true,
useCachedDialogs : false });
// The callback can be trusted only if it say the user is NOT connected!
// * The scenario that the user is connected is covered by the auth.statusChange event
FB.getLoginStatus(function(session) {
console.log("getLoginStatus (just after init) callback. session: " + session.status);
if (session.status === "notConnected") {
statusChangeRaised = true;
handleLoginStatus(d, session, false);
}
});
// The timeout will handle scenario that the user is "unknown", so the auth.StatusChange didn't rise
// and also the getLoginStatus cannot be truested,
setTimeout(function () {
if (!statusChangeRaised) {
FB.Event.unsubscribe('auth.statusChange', statusChangeHandler);
FB.getLoginStatus(function(session) {
console.log('FB.getLoginStatus (inside timeout after FB.init) callback. session: ' + session.status);
handleLoginStatus(d, session, true);
});
}
}, 6 * 1000);