Beispiel #1
0
        // Facebook Authentification
        void FacebookAuthentication()
        {
            flag = false;
            // If was send true to Profile.EnableUpdatesOnAccessTokenChange method
            // this notification will be called after the user is logged in and
            // after the AccessToken is gotten
            Profile.Notifications.ObserveDidChange((sender, e) =>
            {
                if (e.NewProfile == null)
                {
                    return;
                }

                var accessToken = AccessToken.CurrentAccessToken.TokenString;
                var request     = new GraphRequest("/" + e.NewProfile.UserID, new NSDictionary("fields", "email,first_name,last_name,picture.type(large)"), accessToken, null, "GET");
                request.Start((connection, result, error) =>
                {
                    // Show the loading overlay on the UI
                    LoadingOverlay.ShowOverlay(View);

                    var userInfo = result as NSDictionary;

                    // Get Facebook avatar image from url
                    var avatarUrl = new NSUrl(userInfo["picture"].ValueForKey(new NSString("data")).ValueForKey(new NSString("url")).ToString());
                    var data      = NSData.FromUrl(avatarUrl);

                    facebookImageData = data;

                    // string facebookAvatar = data.GetBase64EncodedString(NSDataBase64EncodingOptions.None);
                    // facebookAvatar = new NSString(eefacebookAvatar, NSStringEncoding.UTF8); //Encoding.ASCII.GetString(ToByteArray(data));

                    // Add Facebook user to database
                    var user = new UsersModel()
                    {
                        FirstName     = userInfo["first_name"].ToString(),
                        LastName      = userInfo["last_name"].ToString(),
                        Email         = userInfo["email"].ToString().ToLower(),
                        LoginProvider = "Facebook"
                    };

                    // Call REST service to send Json data
                    RestService rs = new RestService();

                    // Get Json data from server in JsonResponseModel format
                    Task <UsersModel> jsonResponeTask = rs.UserLoginAndRegisterJson(user, "register");

                    // If there was an error in PostJsonDataAsync class, display message
                    if (jsonResponeTask == null)
                    {
                        LoadingOverlay.RemoveOverlay();
                        return;
                    }

                    // Get user id from Json after login or display an error
                    GetUserIdFromJson(jsonResponeTask, user);

                    if (error != null)
                    {
                        LoadingOverlay.RemoveOverlay();
                        Console.WriteLine("Error in request. Start Facebook login");
                        return;
                    }
                });
            });

            // Set the Read and Publish permissions you want to get
            loginView = new LoginButton(fbAuthButton.Frame)
            {
                LoginBehavior   = LoginBehavior.Native,
                ReadPermissions = readPermissions.ToArray()
            };

            // Handle actions once the user is logged in
            loginView.Completed += LoginView_Completed;

            // Handle actions once the user is logged out
            loginView.LoggedOut += (sender, e) =>
            {
                // Set logout User Defaults
                var plist = NSUserDefaults.StandardUserDefaults;
                plist.SetBool(false, "isUserLoggedIn"); // set flag that user logged out
                plist.SetString("", "userId");
                plist.Synchronize();
            };

            // Use your System Account of Settings
            //loginButton.LoginBehavior = LoginBehavior.SystemAccount;

            // Add views to main view
            View.AddSubview(loginView);
        }