在Sencha中“登录facebook”代码!集成到应用程序中

时间:2012-06-10 03:29:42

标签: extjs

我正在使用Sencha创建一个应用程序,我需要将facebook登录集成在其中。当我点击“使用Facebook登录”按钮时,如果用户未登录Facebook,则应显示弹出窗口输入facebook email-id和密码,否则如果用户已登录则应该只签名-in与facebook身份验证。如何使用sencha touch实现此功能。请帮忙 。如果提供此功能的代码将会有所帮助。谢谢

2 个答案:

答案 0 :(得分:4)

首先,您必须使用facebook(please follow this link)创建JavaScript SDK应用。创建应用程序后,您将拥有APP ID。然后将以下代码添加到app.js

Ext.application({
    name: 'FBLogin',
    launch: function() {
        Ext.create('Ext.container.Viewport', {
            items:[
                {
                    xtype: 'panel',
                    html: '<center><div id="fblogin" class="fb-login-button">Login with Facebook</div></center>'
                }
            ],
            listeners:{
                render: function(obj, eOpts){
                    window.fbAsyncInit = Ext.bind(this.onFacebookInit, this);
                    (function(d){
                        var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
                        if (d.getElementById(id)) {return;}
                        js = d.createElement('script'); js.id = id; js.async = true;
                        js.src = "//connect.facebook.net/en_US/all.js";
                        ref.parentNode.insertBefore(js, ref);
                    }(document));
                }
            },
            onFacebookInit: function(){
                console.log('onFacebookInit');
                var me = this;
                FB.init({
                    appId      : 'YOUR_APP_ID',
                    status     : true,
                    xfbml      : true
                });
                FB.Event.subscribe('auth.authResponseChange', Ext.bind(me.onFacebookAuthResponseChange, me));
            },
            onFacebookAuthResponseChange: function(response){
                this.down('panel').setVisible(false);
                alert("Success fully Logged in");
            }
        });
    }
});

当您创建Facebook APP时,会在facebook登录网站下面有一个名为'SiteURL:'的字段。这应该指向您的Web应用程序URL。 (例如:http://example.com

答案 1 :(得分:4)

您可以尝试使用Phonegap Facebook Connect Plugin 0.4.0(稳定版) 并使用Phonegap Plugin Build构建Android,Iphone,Windows Phone,blackbery等软件包

//on Device Ready
    FB.init({ appId: "appid", nativeInterface: CDV.FB, useCachedDialogs: false });
   // call for native app if there else give a popup for login
    FB.login(
                             function(response) {
                             if (response.session) {
                             alert('logged in');
                             } else {
                             alert('not logged in');
                             }
                             },
                             { scope: "email" }
                             );

为了与sencha集成,需要在index.html文件中添加三个脚本文件cdv-plugin-fb-connect.js,phonegap.js和facebook-js-sdk.js,并将config.xml文件添加到项目构建中用于构建本机应用程序的手机间隙构建(不使用sencha本机包装)。对于config.xml文件配置,请参阅此config Help 如有任何疑问,请参阅github示例here 希望它有效