0

I'm using this for my Facebook log-in and sharing. I'm wondering if instead of opening a WebView that displays the log-in with Facebookis there a way when a User have already installed a Facebook app instead of opening the WebView it will opens the Facebook app? And when the User is already log-in in the Facebook App it will gets its credentials and log-in automatically in my app? I can't seem to find how to do this. Thank you in advantage.

Edit

I found out that my activityCode always return -1 instead of >= 0 that's why it always open the WebView instead of the app. And also found out that I need to enabled the Single Sign-On, I enabled the Single Sign-On but it still doesn't open the facebook app. Maybe it is because of FORCE_DIALOG_AUTH that always returns -1. I'm wondering if there is a default value instead of using FORCE_DIALOG_AUTH.

In solution on the FORCE_DIALOG_AUTH I used code below:

Instead of using

facebook.authorize(this, Constants.FACEBOOK_PERMISSIONS,
            Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());

I used

facebook.authorize(this, Constants.FACEBOOK_PERMISSIONS, new LoginDialogListener());

where in my Facebook.java

 public void authorize(Activity activity, String[] permissions,
        final DialogListener listener) {
    authorize(activity, permissions, DEFAULT_AUTH_ACTIVITY_CODE, listener);
}

Also it detects now if there is an Facebook app installed or not, but when there is an Facebook app installed it still doesn't display/open in Facebook app, it just load and goes back to my Activity nothing happens.

Update

I tried to log in without a user log-in in the Facebook app and that user is still not authorized to use my app, it opens the Facebook app log-in screen but after authorizing it, it doesn't get my log-in informations.

Here's my code in Facebook.java it same as it is

private boolean startSingleSignOn(Activity activity, String applicationId,
        String[] permissions, int activityCode) {
    boolean didSucceed = true;
    Intent intent = new Intent();

    intent.setClassName("com.facebook.katana",
            "com.facebook.katana.ProxyAuth");
    intent.putExtra("client_id", applicationId);
    if (permissions.length > 0) {
        intent.putExtra("scope", TextUtils.join(",", permissions));
    }

    // Verify that the application whose package name is
    // com.facebook.katana.ProxyAuth
    // has the expected FB app signature.
    if (!validateActivityIntent(activity, intent)) {
        return false;
    }

    mAuthActivity = activity;
    mAuthPermissions = permissions;
    mAuthActivityCode = activityCode;
    try {
        activity.startActivityForResult(intent, activityCode);
    } catch (ActivityNotFoundException e) {
        didSucceed = false;
    }

    return didSucceed;
}

In my activity that calls the authorizing and handles what to do after authorizing here's my code

private void setFacebookConnection() {

// progressBar.setVisibility(View.VISIBLE);

    facebook = new Facebook(Constants.FACEBOOK_APP_ID);
    facebookAsyncRunner = new AsyncFacebookRunner(facebook);

// facebook.authorize(MerchantDetailsActivity.this, Constants.FACEBOOK_PERMISSIONS, // Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());

    facebook.authorize(MerchantDetailsActivity.this, Constants.FACEBOOK_PERMISSIONS, new LoginDialogListener());

}

private class LoginDialogListener implements Facebook.DialogListener {
    public void onComplete(Bundle values) {

        String token = facebook.getAccessToken();
        long token_expires = facebook.getAccessExpires();
        Log.d(TAG, "AccessToken: " + token);
        Log.d(TAG, "AccessExpires: " + token_expires);

        facebookSharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        facebookSharedPreferences.edit()
                .putLong(Constants.FACEBOOK_ACCESS_EXPIRES, token_expires)
                .commit();
        facebookSharedPreferences.edit()
                .putString(Constants.FACEBOOK_ACCESS_TOKEN, token).commit();
        facebookAsyncRunner.request("me", new IDRequestListener());

        shareFBPost();
    }

It seems that when the user is already authorized it doesn't go inside my LoginDialogListener

natsumiyu
  • 3,217
  • 7
  • 30
  • 54
  • If you implement according to Facebook's documentation, yes. However, it will still open up a WebView if the app isn't installed. It's not very hard to implement as the documentation will guide you along the way to set up. – Zhi Kai Oct 12 '16 at 08:38
  • I implement it using the link I've provided but even if the `Facebook app` is already installed it still opens the `WebView` – natsumiyu Oct 12 '16 at 08:44

2 Answers2

0

If you will use this guide you will be able to open Facebook app for login

After implementing Facebook auth, initialize Facebook SDK in your Application class or in activity which uses Facebook login

// initialize facebook sdk and app events logger
FacebookSdk.sdkInitialize(getApplicationContext());

Then you can use the class below to login

public class FacebookAuth { 
    private static FacebookAuth instance; 
    private OnLoginDataReadyListener mResponseListener; 
    public static synchronized FacebookAuth getInstance() { 
        if (instance == null) { 
            instance = new FacebookAuth(); 
        } 
        return instance; 
    } 

    /** 
     * Call if you want the user to login with his facebook account 
     * @param activity needed to initialize the Facebook LoginManager 
     * @param listener used to set the login listener 
     */ 
     public void facebookLogin(Activity activity, OnLoginDataReadyListener listener, CallbackManager callbackManager) { 
         mResponseListener = listener;
         LoginManager.getInstance().logInWithReadPermissions(activity, Arrays.asList("public_profile", "user_friends", "email"));
         LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
            @Override 
            public void onSuccess(LoginResult loginResult) { 
                getUserData(); 
            } 

            @Override 
            public void onCancel() { 
                if (mResponseListener != null) { 
                    mResponseListener.onCanceled(); 
                } 
            } 

            @Override 
            public void onError(FacebookException error) {
                if (mResponseListener != null) { 
                    mResponseListener.onCanceled(); 
                } 
            } 
         }); 
     } 

    /** 
     * Creates an Facebook Graph request witch will grab the user data 
     * such as name id and picture for now 
     */ 
    public void getUserData() { 
        GraphRequest request = GraphRequest.newMeRequest( AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() { 
            @Override 
            public void onCompleted(JSONObject object, GraphResponse response) { 
                if (mResponseListener != null) {
                    mResponseListener.onLoginDataReady(object);
                }
            } 
        }); 
        Bundle parameters = new Bundle();
        parameters.putString("fields", "picture.height(200).width(200),cover,location,birthday,first_name,last_name,email,gender,name"); 
        request.setParameters(parameters);
        request.executeAsync();
    } 

    public interface OnLoginDataReadyListener { 
        void onLoginDataReady(JSONObject facebookData); 
        void onCanceled(); 
    }
}

Once you've implemented the above solution, în your activity create a CallbackManager

CallbackManager mCallbackManager = CallbackManager.Factory.create();

Then in button click listener you can login your user as following

FacebookAuth.getInstance().facebookLogin(activity, dataReadyListener, mCallbackManager);

And finally in onActivityResult()

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data);
    mCallbackManager.onActivityResult(requestCode, resultCode, data); 
}

Hope this will help you ))

Eduard Albu
  • 301
  • 1
  • 8
0

I use the latest Facebook SDK instead and follow these steps. It is important to add onActivityResult for Facebook login callbackManager.

natsumiyu
  • 3,217
  • 7
  • 30
  • 54