Forum Xamarin.Forms


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

await in iOS device

JayceJayce USMember, University


I am making an app and have an await call in my login page.
Basically the async method is run when a button is clicked and in this method it awaits a login method that verifies and sets my credentials.
This button and the login work absolutely fine in Android and in the iOS iPhone 5S (9.1) simulator.

That's great and all but somehow it doesn't work on a physical device. It seemingly returns, without returning. Here is so code to help explain. This is from the login page:
this.ButtonLogin.Clicked += async(sender, eventArgs) => { this.AILogin.IsVisible = true; var vm = this.BindingContext as LoginViewModel; var go = await vm.Login(this.Username.Text, this.Password.Text); if(go) { this.AILogin.IsVisible = false; RegisterDevice(); //await this.Navigation.PushModalAsync(((MasterDetailPage)new RootPage()), false); var hasPasswordBeenReset = Convert.ToBoolean(App.Current.Properties["BtmPasswordResetFlag"]); if (hasPasswordBeenReset) { await this.Navigation.PushModalAsync(new NavigationPage(new ChangePasswordView()), false); } else { //RegisterDevice(); await this.Navigation.PushModalAsync(new NavigationPage(new MessageListView()), false); } //RegisterDevice(); //await this.Navigation.PushModalAsync(((MasterDetailPage)new RootPage()), false); } else { this.AILogin.IsVisible = false; await DisplayAlert("Authentication Failed", "Try Again", "Try Again"); } };

It seems to set go as false every time the button is pressed.
The login method has several points in which to return, I've set breakpoints on all of them, and none of them are hit. It just returns false every time:
` public async Task Login(string username, string password)
HttpResponseMessage response = null;
if(string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
//set a test default username and password
response = await API.PostSignInAsync(API.Authentication, username, password);
if (!response.IsSuccessStatusCode)
return false;

        JObject array;
            //array = response.Content.ReadAsAs
            array = response.Content.ReadAsAsync<JObject>().Result;
        catch (Exception e)
            return false;

        var model = CreateLoginModel(array);
        return true;

In fact, even setting a break-point after response = await API.PostSignInAsync(API.Authentication, username, password); does not get hit either.
So it seems to be leaving this method prematurely and returning false.**> **

Wondering if anyone else had the same behavior or know what's going on. I also posted this in the iOS forums, but since this is technically Xamarin.Forms I asked if it should be moved here or if I should re-post it here, and still haven't gotten a reply so I'm posting it here as well.

Sign In or Register to comment.