2011-06-17 71 views
0

我的手机应用程序首先需要用户登录Facebook,然后返回到应用程序进一步操作。appcelerator titanium mobile Facebook登录 - 还没有设置Titanium.Facebook.loggedIn登录后

成功登录fb后,预计Titanium.Facebook.loggedIn将为true,并且该应用可以获取Ti.Facebook.uid中的值。

然而,情况并非总是如此。有时,成功登录fb并返回应用程序后,Titanium.Facebook.loggedIn仍然是错误的,肯定无法获取Ti.Facebook.uid。 但是,当用户注销并在同一个应用程序部分内再次登录时,它将正常工作。因此,我的应用有时需要用户登录Facebook两次。

你有什么想法吗?代码如下所示。

var login = Titanium.Facebook.createLoginButton({ 
    top: 100, style:'wide' 
}); 

var actionsView = Ti.UI.createView({ 
    top: 0, left: 0, right: 0, touchEnabled: false, height: 320 
}); 

var loginView = Ti.UI.createView({ 
    top: 0, left: 0, right: 0, visible: !Titanium.Facebook.loggedIn, height: Titanium.Platform.displayCaps.platformHeight,opacity:0.85, backgroundColor:'#fff' 
}); 

loginView.add(login); 

Titanium.Facebook.addEventListener('login', function(e) { 
    if (e.success) { 
     if (Titanium.Facebook.loggedIn){ 
     loginView.hide(); 
     actionsView.touchEnabled=true; 
     }else{ 
     statusAlert = Titanium.UI.createAlertDialog({title:appname,message:'Login failed.'}); 
     statusAlert.show(); 
     } 

    if (e.error) { 
     alert(e.error); 
    } 
}); 

var logout = Titanium.Facebook.createLoginButton({ 
    top: 300, style:'wide', visible: false 
}); 
+0

这是1.6或更高,你有权限问FB设置? – bh88

回答

0

你可以使用Titanium.Facebook.uid代替,这将是这样的:

if (Titanium.Facebook.uid){ 

可能是你所遇到的问题是由于一个老版本的钛。

0

通过Facebook成功登录后,将用户ID保存到全局变量或应用程序属性中。 我会喜欢的应用程序性能,因为除非用户从Facebook注销UID将保持在应用性能 I-E

Ti.App.Properties.setString('uid', Ti.Facebook.uid); 

所以当你使用你的应用程序回来只是检查UID第一。

var fb_id = Ti.App.Properties.getString('uid'); 
if(fb_id){ 
// your code 
} 

并且当您注销清除变量与''或空。

0

可能有点迟了一个额外的答案,但我在iOS上有类似的问题。通过FB登录工作很好,但完全关闭应用后,Facebook模块在询问UID时返回'undefined'。我通过调用未记录的'初始化'方法来解决它。

按照这里的安装指南:https://docs.appcelerator.com/platform/latest/#!/api/Modules.Facebook

然后尝试这个例子

var fb = require('facebook'); 

fb.addEventListener('login', function(e) { 
    if (e.success) { 
     alert('login from uid: ' + e.uid + ', name: ' + JSON.parse(e.data).name); 
     showFriends(); 
    } else if (e.cancelled) { 
     // user cancelled 
     alert('cancelled'); 
    } else { 
     alert(e.error); 
    } 
}); 

fb.initialize(); 

function authorize(e) { 

    fb.authorize(); 
} 

function showFriends() { 

    alert("fb:\n" + fb.uid + "\n" + fb.accessToken + "\n" + fb.expirationDate); 
    fb.requestWithGraphPath('me/friends', { 
     limit : 1000, 
    }, 'GET', function(e) { 
     if (e.success) { 
      // Note: only friends who installed this app are returned 
      var result = JSON.parse(e.result); 

      alert("successfully loaded friends"); 

      // each data (user) has id + name fields 

     } else if (e.error) { 
      alert('fb friends: ' + e.error); 
     } else { 
      alert('Unknown response'); 
     } 
    }); 
} 

if (fb.loggedIn) { 
    $.button.hide(); 
    alert("already logged in"); 
} else { 
    $.button.show(); 
} 

$.index.open(); 

当然,你需要在你看来像

<Button id="button" onClick="authorize" >log in</Button>