1

我建立一个移动网站,通过Facebook权威性与下面的代码的FB.login在移动浏览器不触发

(function (d, s, id) { 
    var js, fjs = d.getElementsByTagName(s)[0]; 
    if (d.getElementById(id)) { 
     return; 
    } 
    js = d.createElement(s); 
    js.id = id; 
    js.src = "//connect.facebook.net/en_US/all.js"; 
    fjs.parentNode.insertBefore(js, fjs); 
}(document, 'script', 'facebook-jssdk')); 

window.fbAsyncInit = function() { 
    FB.init({ 
     appId: '{app_id}', // App ID from the app dashboard 
     channelUrl: '{app url}', // Channel file for x-domain comms 
     status: true, // Check Facebook Login status 
     xfbml: true 
    }); 

    FB.login(function (response) { 
    //FB.login contents 
    }, { 
     scope: '{permissions}' 
    }); 
} 

这适用于桌面浏览器,但在移动浏览器的FB.login没有按”引导用户触发。似乎很多人都有这个问题。根据this堆栈交换问题FB.login不能在window.fbAsyncInit里面解决,解决这个问题的最好方法是一旦加载了所有东西,就有一个触发FB.login的按钮。在我实施它之前,有人可以授权验证这是我的最佳选择。

回答

1

最好的方法是使用FB.getLoginStatus而不是FB.loginfbAsyncInit部分,然后根据当前状态您可以登录用户或使用他的凭据。得到了这样的事情:

window.fbAsyncInit = function() { 
    FB.Event.subscribe('auth.statusChange', function(auth){ 
     if(auth.status==='connected'){ 
      // subscribe to event, and do some magic stuff when status changes 
     } 
    }); 

    FB.init({ 
     appId: fb_config_app_id,  // App ID from the app dashboard 
     channelUrl: fb_config_channel, // Channel file for x-domain comms 
     status: true,     // Check Facebook Login status 
     xfbml: true      // Look for social plugins on the page 
    }); 

    // Load in the user credentials 
    FB.getLoginStatus(function(response){ 
     if (response.status === 'connected') { 
      // hey - user is already connected ! 
     } else { 
      // login user 
     } 
    }); 
} 
+0

@Slavomir嗨,该FB.getLoginStatus功能确实在fbAsyncInit工作因为你已经断言,但是,应该指出的是,最终是要么没有登录或授权还是用户必须去通过FB.Login(据我所知)最好通过不同页面上的按钮触发。 –

+0

Hi @ BenPearce - 你确定吗?我的例子来自现场项目,我使用给定的例子,为我工作:) – Slavomir