且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Facebook统一SDK,“FB.Init()已经被称为”

更新时间:2023-12-06 15:39:58

FB.Init()不能多次调用。这是设计(因为它在Facebook中的GameObjects和Unity内)。相反,请执行以下操作:

  FB.Init(MyOnInitComplete); 

....

public void MyOnInitComplete(){
// FB.IsLoggedIn检查FB.AccessToken和FB.UserId
如果(!FB.IsLoggedIn){
// FB.Init()被调用,但用户仍然没有登录。
FB.Login(< your_permissions_here>);
} else {
//用户登录& FB.Init称为
}
}


I am working with FB Unity SDK, and one constant problem i am facing is to check if FB.Init() has already been called.

Scenario:

  1. There is a fb connect button, user clicks it.
  2. If the user decides to cancel the dialog and click "Fb connect again"

FB sdk throws a notice saying "FB.Init() has already been called. You need to call this only once."

Here is what i am trying to do, but does'nt work

    if(FB.AccessToken=="" || FB.AccessToken==null){
        Debug.Log ("Fb not init(), call it");
        FB.Init(OnInitComplete, null);
    }else{
        Debug.Log ("Facebook already init()");
        OnInitComplete();           
    }

But, obviously this is not working since FB.Init() was called and the user cancelled it. How can i verify if FB.Init() has already been called? But i was expecting accessToken to be null?

And, do these messages effect the submission of the app? Will they be displayed in the production build?

FB.Init() shouldn't be called more than once. This is by design (because it inits Facebook GameObjects and such within Unity). Instead, do this:

FB.Init(MyOnInitComplete);

....

public void MyOnInitComplete() {
    // FB.IsLoggedIn checks for the FB.AccessToken and the FB.UserId
    if(!FB.IsLoggedIn){
        // FB.Init() is called, but user is still not logged in.
        FB.Login("<your_permissions_here>");
    } else {
        // User is logged in & FB.Init is called       
    }
}