Inappbrowser回调

时间:2013-09-05 07:11:17

标签: facebook cordova sencha-touch-2 inappbrowser

我尝试使用inappbrowser在facebook登录后回电话。但它无法检查电子邮件,名称等等。这是我的代码

Inappbrowser致电

function onDeviceReady(){   
     var my_client_id = FBkey,
     my_redirect_uri = "http://www.fastabuy.com/index.php",
     my_type = "user_agent",
     my_display = "touch";

     var authorize_url = FBgraphapiurl+"/oauth/authorize?";
     authorize_url += "client_id=" + my_client_id;
     authorize_url += "&redirect_uri=" + my_redirect_uri;
     authorize_url += "&display=" + my_display;
     authorize_url += "&scope=publish_stream,email,user_likes";
     isfir = "true";

     var ref = window.open(authorize_url, '_blank', 'location=yes');
     ref.addEventListener('loadstop', function facebookLocChanged() {
         facebookLoc(my_redirect_uri)
     });
}

检查回叫的方法

  function facebookLoc(loc){
     if (loc.indexOf("http://www.fastabuy.com/index.php?") > -1){
       alert(loc.indexOf)
     }
  }

如何检查网址loc.indexOf>-1何时关闭inappbrowser。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:7)

1)您需要在InAppBrowser中使用loadstart事件来检查重定向页面。

2)成功登录后,使用close方法关闭InAppBrowser。

3)当你在回调中调用facebookLoc函数时,你应该注意范围,我使用me变量来解决这个问题。

onFacebookLogin: function() {

    var me = this;
    var appInBrowser = window.open(authorize_url, '_blank', 'location=yes');

    appInBrowser.addEventListener('loadstart', function(event) {
         me.facebookLoc(event.url,appInBrowser);
    });    
}

facebookLoc : function(loc,appInBrowser) {
   if (loc.indexOf("www.facebook.com/connect/login_success.html") > -1){
         alert('Login success');
         appInBrowser.close();
   }
}

答案 1 :(得分:4)

此处有一个示例https://github.com/raulduran/facebook-cordova

onDeviceReady: function() {
    app.onFacebookLogin();
},

onFacebookLogin: function() {

    var authorize_url  = "https://m.facebook.com/dialog/oauth?";
        authorize_url += "client_id=" + appId;
        authorize_url += "&redirect_uri=" + redirectUrl;
        authorize_url += "&display=touch";
        authorize_url += "&response_type=token";
        authorize_url += "&type=user_agent";

    if(permissions !== '') {
        authorize_url += "&scope=" + permissions;
    }

    var userDenied = false;
    var appInBrowser = window.open(authorize_url, '_blank', 'location=no');

    appInBrowser.addEventListener('loadstart', function(location) {

        if (location.url.indexOf("access_token") !== -1) {
            // Success
            var access_token = location.url.match(/access_token=(.*)$/)[1].split('&expires_in')[0];
            window.localStorage.setItem('facebook_accessToken', access_token);
            appInBrowser.close();
        }

        if (location.url.indexOf("error_reason=user_denied") !== -1) {
            // User denied
            userDenied = true;
            window.localStorage.setItem('facebook_accessToken', null);
            appInBrowser.close();
        }
    });
}