Hi There i am getting the following exception on my Iphone 6s device. The same code works fine on android and IOS emulator + android device.
Object reference is not set to an instance of an object at
MyApp.Services.AuthenticationService+
C_Async0.MoveNext()
The AuthenticationUser method of the AuthenticationService is a async method as below.
public async Task<bool> AuthenticationUser(string userName, string password)
{
var logger = DependencyService.Get<ILogger>();
var token = await _restClient.GetAuthenticationToken(userName, password);
if (token == null) return false;
Settings.Current.SignIn(token.access_token);
return true;
}
The exception is being thrown at
var token = await _restClient.GetAuthenticationToken(userName, password);
Here is the GetAuthenticationToken method
public async Task<OAuthResponse> GetAuthenticationToken(string emailAddress, string password)
{
OAuthResponse oAuthResponse;
using (var client = new HttpClient())
{
//setup client
client.BaseAddress = new Uri(ApiSettings.ApiBaseUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//setup login data
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", emailAddress),
new KeyValuePair<string, string>("password", password),
});
//send request
HttpResponseMessage responseMessage = await client.PostAsync(ApiResource.OAuthTokenResource, formContent);
//get access token from response body
var responseJson = await responseMessage.Content.ReadAsStringAsync();
oAuthResponse = JsonConvert.DeserializeObject<OAuthResponse>(responseJson);
if (!responseMessage.IsSuccessStatusCode)
{
throw new InvalidCredentialException<OAuthResponse>(oAuthResponse);
}
}
return oAuthResponse;
}
What is very strange is that the null exception is only thrown on IOS Device. This code is working fine on android and IOS emulator . The issue is just with IOS device.