Example #1
0
        public async Task <FacebookResponse <bool> > LoginAsync(string[] permissions, FacebookPermissionType permissionType = FacebookPermissionType.Read)
        {
            var retVal = IsLoginSessionActive;
            FacebookActionStatus status = FacebookActionStatus.Error;

            if (!HasPermissions(permissions))
            {
                var window = UIApplication.SharedApplication.KeyWindow;
                var vc     = window.RootViewController;
                while (vc.PresentedViewController != null)
                {
                    vc = vc.PresentedViewController;
                }

                LoginManagerLoginResult result = await(permissionType == FacebookPermissionType.Read ? loginManager.LogInWithReadPermissionsAsync(permissions, vc) : loginManager.LogInWithPublishPermissionsAsync(permissions, vc));

                if (result.IsCancelled)
                {
                    retVal = false;
                    status = FacebookActionStatus.Canceled;
                }
                else
                {
                    retVal = HasPermissions(result.GrantedPermissions.Select(p => $"{p}").ToArray());

                    NSUserDefaults.StandardUserDefaults.SetString(result.Token.TokenString, FBAccessTokenKey);
                    NSUserDefaults.StandardUserDefaults.SetValueForKey(result.Token.ExpirationDate, FBAccessTokenExpirationDateKey);
                    NSUserDefaults.StandardUserDefaults.SetString(result.Token.UserId, FBUserIdKey);
                    NSUserDefaults.StandardUserDefaults.Synchronize();

                    status = retVal ? FacebookActionStatus.Completed : FacebookActionStatus.Unauthorized;
                }
            }
            else
            {
                status = FacebookActionStatus.Completed;
            }
            var fbArgs = new FBEventArgs <bool>(retVal, status);

            OnLogin(this, fbArgs);

            pendingAction?.Execute();

            pendingAction = null;


            return(new FacebookResponse <bool>(fbArgs));
        }
Example #2
0
        public async Task <FacebookResponse <bool> > LoginAsync(string[] permissions, FacebookPermissionType permissionType = FacebookPermissionType.Read)
        {
            var retVal = IsLoggedIn;
            FacebookActionStatus status = FacebookActionStatus.Error;

            if (!retVal || !HasPermissions(permissions))
            {
                var window = UIApplication.SharedApplication.KeyWindow;
                var vc     = window.RootViewController;
                while (vc.PresentedViewController != null)
                {
                    vc = vc.PresentedViewController;
                }

                LoginManagerLoginResult result = await(permissionType == FacebookPermissionType.Read ? loginManager.LogInWithReadPermissionsAsync(permissions, vc) : loginManager.LogInWithPublishPermissionsAsync(permissions, vc));

                if (result.IsCancelled)
                {
                    retVal = false;
                    status = FacebookActionStatus.Canceled;
                }
                else
                {
                    //result.GrantedPermissions.h
                    retVal = HasPermissions(result.GrantedPermissions.Select(p => $"{p}").ToArray());

                    status = retVal ? FacebookActionStatus.Completed : FacebookActionStatus.Unauthorized;
                }
            }
            else
            {
                status = FacebookActionStatus.Completed;
            }
            var fbArgs = new FBEventArgs <bool>(retVal, status);

            OnLogin(this, fbArgs);

            pendingAction?.Execute();

            pendingAction = null;


            return(new FacebookResponse <bool>(fbArgs));
        }
        public static void Initialize(Activity activity)
        {
            mLogger          = AppEventsLogger.NewLogger(Android.App.Application.Context as Android.App.Application);
            mCallbackManager = CallbackManagerFactory.Create();
            CurrentActivity  = activity;

            loginCallback = new FacebookCallback <LoginResult>
            {
                HandleSuccess = loginResult =>
                {
                    if (loginResult.AccessToken != null)
                    {
                        var fbArgs = new FBEventArgs <bool>(true, FacebookActionStatus.Completed);
                        _onLogin?.Invoke(CrossFacebookClient.Current, fbArgs);
                        _loginTcs?.TrySetResult(new FacebookResponse <bool>(fbArgs));

                        pendingAction?.Execute();

                        pendingAction = null;
                    }
                },
                HandleCancel = () =>
                {
                    var fbArgs = new FBEventArgs <bool>(false, FacebookActionStatus.Canceled, "User cancelled facebook operation");
                    _onLogin?.Invoke(CrossFacebookClient.Current, fbArgs);
                    _loginTcs?.TrySetResult(new FacebookResponse <bool>(fbArgs));
                    //Handle any cancel the user has perform
                    Console.WriteLine("User cancelled facebook login operation");

                    pendingAction?.Execute();

                    pendingAction = null;
                },
                HandleError = loginError =>
                {
                    var fbArgs = new FBEventArgs <bool>(false, FacebookActionStatus.Error, loginError.ToString());
                    _onLogin?.Invoke(CrossFacebookClient.Current, fbArgs);
                    _loginTcs?.TrySetResult(new FacebookResponse <bool>(fbArgs));

                    //Handle any error happends here
                    Console.WriteLine("Operation throws an error: " + loginError.ToString());

                    pendingAction?.Execute();
                    pendingAction = null;
                }
            };


            shareCallback = new FacebookCallback <SharerResult>
            {
                HandleSuccess = shareResult =>
                {
                    if (shareResult.PostId != null)
                    {
                        Dictionary <string, object> parameters = new Dictionary <string, object>();

                        parameters.Add("postId", shareResult.PostId);
                        var fbArgs = new FBEventArgs <Dictionary <string, object> >(parameters, FacebookActionStatus.Completed);
                        _onSharing?.Invoke(CrossFacebookClient.Current, fbArgs);
                        _shareTcs?.TrySetResult(new FacebookResponse <Dictionary <string, object> >(fbArgs));
                    }
                },
                HandleCancel = () =>
                {
                    var fbArgs = new FBEventArgs <Dictionary <string, object> >(null, FacebookActionStatus.Canceled, "User cancelled facebook operation");
                    _onSharing?.Invoke(CrossFacebookClient.Current, fbArgs);
                    _shareTcs?.TrySetResult(new FacebookResponse <Dictionary <string, object> >(fbArgs));
                },
                HandleError = shareError =>
                {
                    var fbArgs = new FBEventArgs <Dictionary <string, object> >(null, FacebookActionStatus.Error, shareError.Message);
                    _onSharing?.Invoke(CrossFacebookClient.Current, fbArgs);
                    _shareTcs?.TrySetResult(new FacebookResponse <Dictionary <string, object> >(fbArgs));
                }
            };

            appInviteCallback = new FacebookCallback <AppInviteDialog.Result>()
            {
                HandleSuccess = Result =>
                {
                    Dictionary <string, object> parameters = new Dictionary <string, object>();
                    var extras = Result.Data;

                    if (extras != null && !extras.IsEmpty)
                    {
                        foreach (var key in extras.KeySet())
                        {
                            parameters.Add(key, $"{extras.Get(key)}");
                        }
                    }

                    var fbArgs = new FBEventArgs <Dictionary <string, object> >(parameters, FacebookActionStatus.Completed);
                    _onAppInvite?.Invoke(CrossFacebookClient.Current, fbArgs);
                    _appInviteTcs?.TrySetResult(new FacebookResponse <Dictionary <string, object> >(fbArgs));
                },
                HandleCancel = () =>
                {
                    var fbArgs = new FBEventArgs <Dictionary <string, object> >(null, FacebookActionStatus.Canceled, "User cancelled facebook operation");
                    _onAppInvite?.Invoke(CrossFacebookClient.Current, fbArgs);
                    _appInviteTcs?.TrySetResult(new FacebookResponse <Dictionary <string, object> >(fbArgs));
                },
                HandleError = appInviteError =>
                {
                    var fbArgs = new FBEventArgs <Dictionary <string, object> >(null, FacebookActionStatus.Error, appInviteError.Message);
                    _onAppInvite?.Invoke(CrossFacebookClient.Current, fbArgs);
                    _appInviteTcs?.TrySetResult(new FacebookResponse <Dictionary <string, object> >(fbArgs));
                }
            };

            LoginManager.Instance.RegisterCallback(mCallbackManager, loginCallback);
        }
        public async Task <FacebookResponse <bool> > LoginAsync(string[] permissions, FacebookPermissionType permissionType = FacebookPermissionType.Read)
        {
            _loginTcs = new TaskCompletionSource <FacebookResponse <bool> >();
            var retVal = IsLoginSessionActive;
            FacebookActionStatus status = FacebookActionStatus.Error;

            if (!HasPermissions(permissions))
            {
                var window = UIApplication.SharedApplication.KeyWindow;
                var vc     = window.RootViewController;
                while (vc.PresentedViewController != null)
                {
                    vc = vc.PresentedViewController;
                }

                loginManager.LogIn(permissions, vc, (result, error) =>
                {
                    if (error == null)
                    {
                        if (result.IsCancelled)
                        {
                            retVal = false;
                            status = FacebookActionStatus.Canceled;
                        }
                        else
                        {
                            retVal = HasPermissions(result.GrantedPermissions.Select(p => $"{p}").ToArray());

                            NSUserDefaults.StandardUserDefaults.SetString(result.Token.TokenString, FBAccessTokenKey);
                            NSUserDefaults.StandardUserDefaults.SetValueForKey(result.Token.ExpirationDate, FBAccessTokenExpirationDateKey);
                            NSUserDefaults.StandardUserDefaults.SetString(result.Token.UserId, FBUserIdKey);
                            NSUserDefaults.StandardUserDefaults.Synchronize();

                            status = retVal ? FacebookActionStatus.Completed : FacebookActionStatus.Unauthorized;
                        }
                    }
                    else
                    {
                        retVal = false;
                        status = FacebookActionStatus.Error;
                    }



                    var fbArgs = new FBEventArgs <bool>(retVal, status);
                    OnLogin?.Invoke(CrossFacebookClient.Current, fbArgs);
                    _loginTcs?.TrySetResult(new FacebookResponse <bool>(fbArgs));

                    pendingAction?.Execute();

                    pendingAction = null;
                });
            }
            else
            {
                var fbArgs = new FBEventArgs <bool>(retVal, FacebookActionStatus.Completed);
                OnLogin?.Invoke(CrossFacebookClient.Current, fbArgs);
                _loginTcs?.TrySetResult(new FacebookResponse <bool>(fbArgs));

                pendingAction?.Execute();

                pendingAction = null;
            }

            return(await _loginTcs.Task);
        }