protected async override void OnAppearing() { var session_exists = LoadSession(); if (session_exists) { //Move to HomePage DeactivateBusyIndicator(); var home_page_instance = new InstagramProfilePage(); home_page_instance.InstaApi = InstaApi; Navigation.InsertPageBefore(home_page_instance, this); await Navigation.PopAsync(); } else { content.IsVisible = true; DeactivateBusyIndicator(); return; } }
private async void Login_pressed(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(username_field.Text) || string.IsNullOrWhiteSpace(PasswordEntry.Text)) { await DisplayAlert("Error", "Please type username and password", "Ok"); return; } ActivateBusyIndicator(); //get data from fields var userSession = new UserSessionData { UserName = username_field.Text, Password = PasswordEntry.Text }; InstaApi = InstaApiBuilder.CreateBuilder() .SetUser(userSession) .UseLogger(new DebugLogger(LogLevel.All)) .SetRequestDelay(RequestDelay.FromSeconds(0, 1)) .Build(); if (!InstaApi.IsUserAuthenticated) { var logInResult = await InstaApi.LoginAsync(); if (logInResult.Succeeded) { //save user session SaveSession(); //Move to HomePage DeactivateBusyIndicator(); var home_page_instance = new InstagramProfilePage(); home_page_instance.InstaApi = InstaApi; Navigation.InsertPageBefore(home_page_instance, this); await Navigation.PopAsync(); } else { // two factor is required if (logInResult.Value == InstaLoginResult.TwoFactorRequired) { PromptResult pResult = await UserDialogs.Instance.PromptAsync(new PromptConfig { InputType = InputType.Number, OkText = "Login", Title = "Two Factor Authentication", }); if (pResult.Ok && !string.IsNullOrWhiteSpace(pResult.Text)) { if (InstaApi == null) { return; } var twoFactorLogin = await InstaApi.TwoFactorLoginAsync(pResult.Text); if (twoFactorLogin.Succeeded) { // connected // save session SaveSession(); //Move to HomePage DeactivateBusyIndicator(); var home_page_instance = new InstagramProfilePage(); home_page_instance.InstaApi = InstaApi; Navigation.InsertPageBefore(home_page_instance, this); await Navigation.PopAsync(); } else { await DisplayAlert("Error", "There has been an error in login", "Ok"); } } } else { await DisplayAlert("Error", logInResult.Info.Message, "Ok"); } } } else { await DisplayAlert("Info", "Already Connected", "Ok"); } }