1

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?

Steven Anderson
  • 8,398
  • 4
  • 27
  • 32
  • This [link](http://stackoverflow.com/questions/28305642/request-changed-from-post-to-options-hangs) might help. – frank Feb 09 '15 at 08:31
  • Thanks @frank but was unrelated to my issue. I found the answer to my question and have posted below. Thanks. – Steven Anderson Feb 10 '15 at 01:30

1 Answers1

4

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.

Community
  • 1
  • 1
Steven Anderson
  • 8,398
  • 4
  • 27
  • 32
  • Is it just in iOS that basic authentication is not working? I also found that if incorrect credentials are entered as the basic authentication headers then this is the bit that triggers the never-ending-response, as the browser is then trying to challenge you to enter correct credentials, but you can't because the pop up never appears. See my edit in my answer above. – Steven Anderson Sep 03 '15 at 22:49
  • this was driving me crazy!! my app would intermittently never complete requests, I couldn't figure it out - and a clean wipe and reinstall would fix it - this was why. Now to figure out what to do instead... – BIU Oct 21 '15 at 13:39