1

我正在构建一个移动网站,该网站使用以下代码通过 Facebook auth 引导用户

(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 不会触发。好像很多人都遇到过这个问题。根据这个Stack Exchange 问题 FB.login 不能在 window.fbAsyncInit 内触发,解决这个问题的最好方法是有一个按钮,一旦加载了所有内容,就会触发 FB.login。在我实施之前,权威人士可以验证这是我的最佳选择吗?

4

1 回答 1

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
        }
    });
}
于 2013-07-08T03:38:46.867 回答