Steven Anderson
Steven Anderson

Reputation: 8498

Phonegap iOS ajax request never completes

I have a phonegap app which connects to a web service and authenticates using http basic authentication. It is built using phonegap build and targets Android and iOS.

On a login view, an ajax request fires against the remote server to check if credentials are correct, then if so, logs the user in to the main application.

This completes successfully in ripple emulator on desktop pc and when also when deployed onto an Android device.

However, when the app is deployed onto an iOS device (ipod touch) the authentication request simply does not ever complete. Using phonegap remote debugger I can see that the ajax request starts but never completes. It is always in a pending state. I use jquery ajax success, error and complete handlers for the request, but none of them are ever hit so I don't get the chance to see any error messages returned from the server. The request never seems to complete.

I have tried making ajax requests to other remote web sites to test that my app can communicate and they succeed, so it doesn't seem as though I have white-listing issues.

Any ideas of what the issue could be?

Upvotes: 1

Views: 3626

Answers (1)

Steven Anderson
Steven Anderson

Reputation: 8498

Please read the update to this answer at bottom.


Original answer

I have found what the issue is and managed to get basic authentication working.

The issue was that the web server was expecting basic authentication details to be preemptively sent with the request.

To do this use the 'headers' property of jquery ajax as shown below:

$.ajax
({
  type: "GET",
  url: "https://webserver/authenticate",
  headers: {
    "Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
  }
})
.done(function(){
    alert('Authenticated!')
})
.fail(function(){
    alert('Error!')
});

See related answer: https://stackoverflow.com/a/11960692/1463497


Update

I found in the end that there is no reliable way of using basic authentication in a web view in iOS. I.e everything is fine if correct credentials are entered but when they are not and the 401 challenge response comes along, iOS web view can't seem to handle it.

In the end, the web service authentication was implemented by simply passing 'username' and 'password' parameters as part of the url:

url: "https://webserver/authenticate?username=abc&password=123"

This is the only consistent way I found of getting authentication to work across iOS and Android in a web view (by getting rid of basic authentication altogether). This of course meant updating the web service itself to accept authentication in this way.

Upvotes: 4

Related Questions