我做了两次测试
当我没有登录Facebook时,我获得成功并且在initHandler中都失败为null,所以我在其他情况下使用Facebook.login。然后提示用户登录,但在填写用户名和密码后,我的loginHandler没有被调用,我的应用程序将如何知道他已登录,因此我可以继续玩游戏,它是如何工作的?请指教。
private function connect(id:String):void {
Security.loadPolicyFile("https://fbcdn-profile-a.akamaihd.net/crossdomain.xml");
Facebook.init(id, initHandler);
}
private function initHandler(success:Object, fail:Object):void { //success, fail are null
if(success){
} else {
Facebook.login(loginHandler,{scope:"email, user_location"});
}
}
private function loginHandler(success:Object, fail:Object):void {
if(success) {
} else {
//permissions not given
}
}
答案 0 :(得分:0)
你确定没有回复吗?
也许这是语法问题 - loginHandler(结果:对象,失败:对象)......?
如果确实得到了回复,并且返回的对象就是问题,那么可以使用Facebook.getAuthResponse()
来解决它。protected function loginHandler(result:Object, fail:Object):void
{
if (result) {
// --> LOGIN SUCCESS
var resp:String = Facebook.getAuthResponse().toString();
var fbid:String = resp.substr(1, resp.length-2).split(":")[1];
if(id == "null"){
// un-authorized. probably you're in sandbox mode and the user is not a registered tester
}else{
// continue using 'fbid'
}
} else {
// --> LOGIN FAILED
}
}
答案 1 :(得分:0)
init回调永远不会为我开火。它已经使用了很多个月,但现在对成功和失败对象都返回null。您可以使用getAuthResponse测试用户ID,如adifmac所述。我是这样做的:
// in the constructor or wherever, init Facebook without passing an onInitComplete callback
// then call function to check for session.
Facebook.init( APP_ID );
checkForActiveSession();
private function checkForActiveSession():void
{
try
{
// looking for a user ID string from an active session
var hasSession = Facebook.getAuthResponse().uid;
if ( hasSession == null )
{
trace('null session, NOT logged in or has not given permissions');
_userIsLoggedIn = false;
}
else
{
trace('active session, user logged in');
_userIsLoggedIn = true;
// now you can get user data
getUserData();
}
}
catch(e:Error)
{
trace('must be uploaded to server to test : ' + e.message);
}
}
此方法不会具体告诉您它们是否未登录,因为如果用户未授予应用程序权限,此方法也会显示空会话。尽管如此,我发现它的效果非常好。此时请注意,Facebook已初始化,并且对Facebook.login的调用应该有效。
答案 2 :(得分:0)
在我的问题中提到第2点,我没有调用loginHandler,我发现另一篇文章说延迟登录调用,这是要运行的完整代码序列。
public function authorize():void {
var response:FacebookAuthResponse = Facebook.getAuthResponse();
if(response && response.uid) {
this.success(response);
} else {
this.init();
}
}
protected function init():void {
Security.loadPolicyFile("https://fbcdn-profile-a.akamaihd.net/crossdomain.xml");
Facebook.init(this.appID, this.initHandler);
}
protected function initHandler(response:FacebookAuthResponse, fail:Object):void {
if(response && response.uid){
this.success(response);
} else {
setTimeout(this.login, 200);
}
}
protected function login():void {
Facebook.login(loginHandler, this.permissions);
}
protected function loginHandler(response:FacebookAuthResponse, fail:Object):void {
if(response && response.uid) {
this.success(response);
} else {
//not authorized
}
}
protected function success(response:FacebookAuthResponse):void {
//authorized
}