public iOSFacebookProbe()
 {
     _loginManager = new LoginManager();
     _loginResultHandler = new LoginManagerRequestTokenHandler((loginResult, error) =>
         {
             if (error == null && loginResult.Token != null)
                 SensusServiceHelper.Get().Logger.Log("Facebook login succeeded.", SensusService.LoggingLevel.Normal, GetType());
             else if (loginResult != null && loginResult.IsCancelled)
                 SensusServiceHelper.Get().Logger.Log("Facebook login cancelled.", SensusService.LoggingLevel.Normal, GetType());
             else
                 SensusServiceHelper.Get().Logger.Log("Facebook login failed.", SensusService.LoggingLevel.Normal, GetType());
         });
 }
Exemple #2
0
 public iOSFacebookProbe()
 {
     _loginManager       = new LoginManager();
     _loginResultHandler = new LoginManagerRequestTokenHandler((loginResult, error) =>
     {
         if (error == null && loginResult.Token != null)
         {
             SensusServiceHelper.Get().Logger.Log("Facebook login succeeded.", SensusService.LoggingLevel.Normal, GetType());
         }
         else if (loginResult != null && loginResult.IsCancelled)
         {
             SensusServiceHelper.Get().Logger.Log("Facebook login cancelled.", SensusService.LoggingLevel.Normal, GetType());
         }
         else
         {
             SensusServiceHelper.Get().Logger.Log("Facebook login failed.", SensusService.LoggingLevel.Normal, GetType());
         }
     });
 }
Exemple #3
0
        private void ObtainAccessToken(string[] permissionNames)
        {
            lock (LoginLocker)
            {
                if (HasValidAccessToken)
                {
                    SensusServiceHelper.Get().Logger.Log("Already have valid Facebook access token. No need to initialize.", LoggingLevel.Normal, GetType());
                    return;
                }

                ManualResetEvent loginWait = new ManualResetEvent(false);
                bool loginCancelled = false;
                string accessTokenError = null;

                Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                    {
                        try
                        {
                            LoginManagerRequestTokenHandler loginResultHandler = new LoginManagerRequestTokenHandler((loginResult, error) =>
                                {
                                    if (error == null && loginResult.Token != null)
                                    {
                                        SensusServiceHelper.Get().Logger.Log("Facebook login succeeded.", SensusService.LoggingLevel.Normal, GetType());
                                        AccessToken.CurrentAccessToken = loginResult.Token;
                                        loginWait.Set();
                                    }
                                    else if (loginResult != null && loginResult.IsCancelled)
                                    {
                                        SensusServiceHelper.Get().Logger.Log("Facebook login cancelled.", SensusService.LoggingLevel.Normal, GetType());
                                        loginCancelled = true;
                                        loginWait.Set();
                                    }
                                    else
                                    {
                                        SensusServiceHelper.Get().Logger.Log("Facebook login failed.", SensusService.LoggingLevel.Normal, GetType());
                                        loginWait.Set();
                                    }
                                });

                            new LoginManager().LogInWithReadPermissions(permissionNames, UIApplication.SharedApplication.KeyWindow.RootViewController, loginResultHandler);
                        }
                        catch (Exception ex)
                        {
                            SensusServiceHelper.Get().Logger.Log("Error while initializing Facebook SDK and/or logging in:  " + ex.Message, LoggingLevel.Normal, GetType());
                            accessTokenError = ex.Message;
                            loginWait.Set();
                        }
                    });

                loginWait.WaitOne();

                if (accessTokenError != null)
                    SensusServiceHelper.Get().Logger.Log("Error while initializing Facebook SDK and/or logging in:  " + accessTokenError, LoggingLevel.Normal, GetType());

                if (!HasValidAccessToken)
                {
                    string message = "Failed to obtain access token.";
                    SensusServiceHelper.Get().Logger.Log(message, LoggingLevel.Normal, GetType());

                    // if the user cancelled the login, don't prompt them again
                    if (loginCancelled)
                        throw new NotSupportedException(message + " User cancelled login.");
                // if the user did not cancel the login, allow the login to be presented again when the health test is run
                else
                        throw new Exception(message);
                }
            }
        }
Exemple #4
0
        private void ObtainAccessToken(string[] permissionNames)
        {
            lock (LoginLocker)
            {
                if (HasValidAccessToken)
                {
                    SensusServiceHelper.Get().Logger.Log("Already have valid Facebook access token. No need to initialize.", LoggingLevel.Normal, GetType());
                    return;
                }

                ManualResetEvent loginWait        = new ManualResetEvent(false);
                bool             loginCancelled   = false;
                string           accessTokenError = null;

                SensusContext.Current.MainThreadSynchronizer.ExecuteThreadSafe(() =>
                {
                    try
                    {
                        LoginManagerRequestTokenHandler loginResultHandler = new LoginManagerRequestTokenHandler((loginResult, error) =>
                        {
                            if (error == null && loginResult.Token != null)
                            {
                                SensusServiceHelper.Get().Logger.Log("Facebook login succeeded.", Shared.LoggingLevel.Normal, GetType());
                                AccessToken.CurrentAccessToken = loginResult.Token;
                                loginWait.Set();
                            }
                            else if (loginResult != null && loginResult.IsCancelled)
                            {
                                SensusServiceHelper.Get().Logger.Log("Facebook login cancelled.", Shared.LoggingLevel.Normal, GetType());
                                loginCancelled = true;
                                loginWait.Set();
                            }
                            else
                            {
                                SensusServiceHelper.Get().Logger.Log("Facebook login failed.", Shared.LoggingLevel.Normal, GetType());
                                loginWait.Set();
                            }
                        });

                        new LoginManager().LogInWithReadPermissions(permissionNames, UIApplication.SharedApplication.KeyWindow.RootViewController, loginResultHandler);
                    }
                    catch (Exception ex)
                    {
                        SensusServiceHelper.Get().Logger.Log("Error while initializing Facebook SDK and/or logging in:  " + ex.Message, LoggingLevel.Normal, GetType());
                        accessTokenError = ex.Message;
                        loginWait.Set();
                    }
                });

                loginWait.WaitOne();

                if (accessTokenError != null)
                {
                    SensusServiceHelper.Get().Logger.Log("Error while initializing Facebook SDK and/or logging in:  " + accessTokenError, LoggingLevel.Normal, GetType());
                }

                if (!HasValidAccessToken)
                {
                    string message = "Failed to obtain access token.";
                    SensusServiceHelper.Get().Logger.Log(message, LoggingLevel.Normal, GetType());

                    // if the user cancelled the login, don't prompt them again
                    if (loginCancelled)
                    {
                        throw new NotSupportedException(message + " User cancelled login.");
                    }
                    // if the user did not cancel the login, allow the login to be presented again when the health test is run
                    else
                    {
                        throw new Exception(message);
                    }
                }
            }
        }