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:
There is a fb connect button, user clicks it.
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
}
}
Related
I'm using Unity3D 2018.2.6f1 With Facebook SDK.
This is the code I'm using to login into facebook once the user clicked on the login button:
FB.Init(()=>FB.LogInWithReadPermissions
(
new List<string>() { "public_profile", "email" },
result_ =>
{
//....
}
));
I'm testing the login behavior on an android device.
The first time I log in it requires a username and a password which is great.
But each time afterward it will prompt me with a message that says:
"You previously logged in to myAppName with Facebook. Would you like to continue?"
And it lets me choose between "continue" and "cancel".
Is there a way to avoid this message?
In the game "Coin Master" for example, I don't see this message every time I log into the game after I already logged in once with facebook.
The facebook sdk has a IsLoggedIn method to determine if someone is already logged. You might want to call it before doing your loginwithreadpermission call.
Maybe something like this:
private void OnFacebookInitialized()
{
if (!FB.IsLoggedIn)
// you are not logged, call loginWithReadPermission
else
// you are already logged in, do something
}
// ...
FB.Init(OnFacebookInitialized); // init FB
See documentation of FB Session Properties for more information
I am currently developing an app on Android using phonegap and would like to use Facebook in the app. I have created an app on Facebook called say "Test". When I am not logged into "Test" but I am logged into Facebook (Facebook app on the Android device) and run the getLoginStatus() function I get a response of unknown instead of not_authorized. Thus my first question is this intended? Am I meant to get a response of unknown or is this an error from phonegap and I'm meant to get a response of not_authorized.
The second question is, when I login into the Facebook app called "test" everything runs as it should (Facebook user gets registered for "test"). When I call the getLoginStatus() function it works perfectly fine and says I'm connected with no problems.
BUT
If I go into Facebook and remove the app "test" from my FB account and then run the android app again and run the getLoginStatus() function is STILL says that I have the app "test" registered to my Facebook account but this is no the case!! Note that I do in the code make sure that I call the server every time instead of using a cached result by the JS SDK. Now my Second question is. Is this meant to happen? Or if not is it a Facebook or Phonegap problem (Phonegap either keeps getting the cached response or Facebook doesn't update their side quick enough?)
Here is code:
function getLoginStatus()
{
FB.getLoginStatus(function(response)
{
alert(JSON.stringify(response));
alert(response.status);
if (response.status == 'connected')
{
alert('logged in');
FB.api('/me',function (response) {
if (response.error)
{
alert('There is an error');
}
else
{
alert(response.name);
}
});
}
else
{
alert('not logged in');
}
},true);
}
Any response would really really be appreciated!!
When I am not logged into "Test" but I am logged into Facebook (Facebook app on the Android device) and run the getLoginStatus() function I get a response of unknown instead of not_authorized.
not_authorized: You get not_authorized when the user is logged into facebook but has not authorized your app. If you transfer the guy to facebook homepage, he enters username and password and cancels authorization, thereafter you get not authorized. IF the guy is logged into facebook,and your app hasn't transferred him to fb to authorize yet, you still get not authorized
https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
unknown: If the user is not logged into facebook, you get a status of unknown.
So logged in, not authorized : not_authorized
Not Logged in: unknown
If I go into Facebook and remove the app "test" from my FB account
I am guessing it takes a while for these changes to propogate. If the user has authorized the app in the current context, it will stay authorized for a while(or until he logs off). I don't think there's anything wrong with this personally
This is not a phonegap issue. unknown is one of the valid responses from the getLoginStatus function. See the facebook API documentation for information about this funciton. There, they describe the unknown response as:
the user is not logged into Facebook at this time and so we don't know if they've authenticated your application or not (unknown)
As to your second question, the problem you are seeing is because by default, the getLoginStatus function caches its response for performance reasons. However, they provide you with a way to force a refresh of the status. You can do this by passing a second parameter, Force into getLoginStatus.
FB.getLoginStatus(function(response) {
// this will be called when the roundtrip to Facebook has completed
}, true); // The second parameter, called 'force' by default is False. True signifies that you want to force a call to the server so you can see the latest login status.
You can read more about that Force parameter by looking at the above API documentation link and searching for the 'Force' parameter.
By the way, I had temporarily deleted the answer as I had answered without knowing how to respond to the second question, but undeleted it after finding out the reason behind your issue.
I am using facebooksdk.framework 3.1
when I login my app with my account, native login dialog pops up even though web based auth
completed. I need to turn off either native login dialog or web based auth but I don't know how.
I tried to find facebook.m file but there was no such file on facebooksdk.framework 3.1
how do I turn off safariauth or native auth?? It is very strange two login process occurs at the
same time.
First of all go to facebook.m page & try to find the following method:
- (void)authorize:(NSArray *)permissions
delegate:(id<FBSessionDelegate>)delegate
localAppId:(NSString *)localAppId
in this method there is a line:[self authorizeWithFBAppAuth:YES safariAuth:NO];
change authorizeWithFBAppAuth & safariAuth to yes/no according to your need.Hope it helps you.
Or you can try to implement latest Share kit sdk.it is best way.
If you use phonegap facebook plugin, you should call FB.getLoginStatus() carefully.
If FB.getLoginStatus() fires before FB.init() is done, the function would return a response as
'not connected' even if user is already connected.
my login problem was due to FB.getLoginStatus() on my redirected page.
even if user succed login and procceded to my redirected page, the page run FB.getLoginStatus()
and result was always 'not connected' because FB.init() not completely executed.
facebook provides async function to solve this problem but it didn't work when I tested on phonegap.
to check user login, I use FB.Event.subscribe and localstorage for now.
on the page that needs FB functions, I added this javascript code
FB.Event.subscribe('auth.login', function(response){
localStorage.setItem('fblogin', true);
console.log('login event');
}
FB.Event.subscribe('auth.logout', function(response){
localStorage.setItem('fblogin', false);
console.log('logout event');
}
document.addEventListener('deviceready', function(){
try{
FB.init({ appId : '1234567889' , nativeInterface : CDV.FB, useCachedDialogs : true});
}catch(e){
alert(e);
}
var fbval = localStorage.getItem('fblogin');
if(fbval){
// your code for connected status
}
});
basically, I set 'fblogin' localStorage value true whenever login event occurs,
and set the value 'false' whenever logout event occurs.
by comparing 'fblogin' value, I check users are logged or not.
I have upgraded my js FB Connect to oauth version & when I am trying to logout from FB programatically using FB.logout() method, I am getting error like
"FB.logout() called without an access token"
What is the problem behind this? I saw one thread over here but it didn't worked for me. Please help me if someone has found solution for this. Thanks.
This is what i've used before.
//check if logout is
FB.getLoginStatus(function(ret) {
/// are they currently logged into Facebook?
if(ret.authResponse) {
//they were authed so do the logout
FB.logout(function(response) {
//do your stuff here.
});
} else {
///do something if they aren't logged in
//or just get rid of this if you don't need to do something if they weren't logged in
}
});
I've encountered this problem and fixed it.
This happened for me when a user had already logged out and I tried to fb.logout() method again. It seems in the following code:
FB.logout(function(response)
{
console.log(response.status);
}
);
response.status will say 'connected' even if the user is logged out, due to some caching issues or some other bug. Hence its better to use authResponse to determine whether or not the user is logged in. I.e:
FB.logout(function(response)
{
if (! response.authResponse)
//disable logout button
}
);
I am using FB's Javascript SDK using the login-button:
<fb:login-button onlogin="onFBLogin();">
How can I detect within onFBLogin() if the user did log in successfully ? Currently it is called everytime, no matter what the user chooses.
Thanks,
Meir
I think the first parameter passed into onFBLogin() is going to be either a response or a session object. So check to see what the value of response or response.session is.
Also, have you played around with FBJS's login event handling?
If using FB.login:
FB.login(function(response) {
if (response.session) {
// user successfully logged in
} else {
// user cancelled login
}
});
But since you're using an fb:login button, you can try to subscribe to the login event:
FB.Event.subscribe('auth.login', function(response) {
// do something with response.session
});
I haven't tried this myself via fb:login button, but hopefully this will get you on the right track.