IT虾米网

javascript之JS Facebook登录iOS8

mfryf 2025年05月04日 程序员 24 0

我的 facebook 应用程序上的登录按钮在 iOS 8 中完全停止工作。我以为这是我所做的事情,但是当我从他们的网站获取 facebook 示例 html 并将其应用到我的页面时,它仍然不起作用(我的应用程序 ID已替换为 xxxxx)。基本上,弹出窗口会打开进行 Facebook 身份验证,但永远不会关闭并返回到我的应用程序。我只是留下一个空白,打开哪个选项卡:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Facebook Login JavaScript Example</title> 
    <meta charset="UTF-8"> 
</head> 
<body> 
    <script> 
        // This is called with the results from from FB.getLoginStatus(). 
        function statusChangeCallback(response) { 
            console.log('statusChangeCallback'); 
            console.log(response); 
            // The response object is returned with a status field that lets the 
            // app know the current login status of the person. 
            // Full docs on the response object can be found in the documentation 
            // for FB.getLoginStatus(). 
            if (response.status === 'connected') { 
                // Logged into your app and Facebook. 
                testAPI(); 
            } else if (response.status === 'not_authorized') { 
                // The person is logged into Facebook, but not your app. 
                document.getElementById('status').innerHTML = 'Please log ' + 
                  'into this app.'; 
            } else { 
                // The person is not logged into Facebook, so we're not sure if 
                // they are logged into this app or not. 
                document.getElementById('status').innerHTML = 'Please log ' + 
                  'into Facebook.'; 
            } 
        } 
 
        // This function is called when someone finishes with the Login 
        // Button.  See the onlogin handler attached to it in the sample 
        // code below. 
        function checkLoginState() { 
            FB.getLoginStatus(function (response) { 
                statusChangeCallback(response); 
            }); 
        } 
 
        window.fbAsyncInit = function () { 
            FB.init({ 
                appId: 'xxxxxxxxxxxxx', 
                cookie: true,  // enable cookies to allow the server to access 
                // the session 
                xfbml: true,  // parse social plugins on this page 
                version: 'v2.1' // use version 2.1 
            }); 
 
            // Now that we've initialized the JavaScript SDK, we call 
            // FB.getLoginStatus().  This function gets the state of the 
            // person visiting this page and can return one of three states to 
            // the callback you provide.  They can be: 
            // 
            // 1. Logged into your app ('connected') 
            // 2. Logged into Facebook, but not your app ('not_authorized') 
            // 3. Not logged into Facebook and can't tell if they are logged into 
            //    your app or not. 
            // 
            // These three cases are handled in the callback function. 
 
            FB.getLoginStatus(function (response) { 
                statusChangeCallback(response); 
            }); 
 
        }; 
 
        // Load the SDK asynchronously 
        (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/sdk.js"; 
            fjs.parentNode.insertBefore(js, fjs); 
        }(document, 'script', 'facebook-jssdk')); 
 
        // Here we run a very simple test of the Graph API after login is 
        // successful.  See statusChangeCallback() for when this call is made. 
        function testAPI() { 
            console.log('Welcome!  Fetching your information.... '); 
            FB.api('/me', function (response) { 
                console.log('Successful login for: ' + response.name); 
                document.getElementById('status').innerHTML = 
                  'Thanks for logging in, ' + response.name + '!'; 
            }); 
        } 
    </script> 
    <!-- 
      Below we include the Login Button social plugin. This button uses 
      the JavaScript SDK to present a graphical Login button that triggers 
      the FB.login() function when clicked. 
    --> 
 
    <fb:login-button scope="public_profile,email" onlogin="checkLoginState();"> 
    </fb:login-button> 
 
    <div id="status"> 
    </div> 
 
</body> 
</html> 

请您参考如下方法:

我想到的唯一解决方案是在 iOS 上使用完全重定向时降级回服务器端身份验证流程。基本上你的应用程序应该点击这样的 URL:

https://www.facebook.com/dialog/oauth? 
    client_id={app-id} 
   &redirect_uri={redirect-uri} 

这在桌面上是一种不和谐的体验,但在移动设备上可以说是一种更好的体验,因为用户不会经历奇怪的选项卡切换(这首先是新问题的根源)。要仅在 iOS 上降级到此流程,请确保您的登录链接实际上是 Facebook 身份验证对话框的 href(如上面的链接,或者对于 Rails 上的omniauth 用户来说,是“/auth/facebook”)。然后将调用客户端流程的 JavaScript 封装在代码中,以防止其在 iOS(或所有移动设备,如果您愿意)上运行。

if(!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) { 
[Facebook client side flow code here] 
} 

(如果有人有更优雅的方法,请在评论中告诉我)


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!