IOS Simulator Login issue

I have an ionic 4 app that uses HttpClient from @angular/common/http version 5.0. It’s connecting to a spring REST server to get data. The login and subsequent REST calls work fine in all environments except for IOS Simulator. I wouldn’t care if it works on Simulator since I use the combination of ionic serve and an actual device to test, but it seems like the itunes approves also use simulators because they are rejecting the app saying that they cannot login.

The issue isn’t logging in, login is successful. It seems like for some reason the session is being lost in between login and the next few REST calls. If there’s something wrong with my code or setup, wouldn’t it fail on other systems?

This works on:
IOS, actual devices
Android, actual devices and emulators
Browser, ionic serve

Doesn’t work on:
IOS Simulator

Call after login:

            let url = SERVER_URL+"/getEmp";
            return this.httpClient.get(url, {withCredentials: true});

Response:

ERROR: In handle error. Http failure response for http://192.168.1.156:8080/getEmp: 401 Unauthorized
2018-08-06 23:02:30.532936-0400 My Mobile App[] ERROR: Unauthorized

Any idea why IOS simulator is different than all devices?

As long as I remember there was a bug in a preview of Xcode 10.beta.something regarding http requests … are you maybe using such a beta version of Xcode?

I’m using Xcode Version 9.4.1 (9F2000)

Well so that’s not the reason. I don’t know, specially because you say it works on a real iPhone and it doesn’t in the simulator…which is weird…same iOS version?

Same iOS version

I think that for some reason some headers are not being sent from the simulator but I’m not sure what that is. I’m trying to intercept all requests using fiddler or Charles proxies but for some reason none of them are capturing requests from the simulator. Once I could get the proxy to work I could start analyzing the requests to see what is the difference between requests coming from the browser and requests coming from the simulator

maybe stupid question, but did you check that your iOS simulator has access to the web? when you open on it safari and try to access google it works too?

inspect ur simulator in safari and paste exact error from console

Thanks for the hint to use Safari. I now have a little more details on the issue

This is a CORS issue, when I change the REST endpoint URL to localhost everything is working fine. When I change it to use an IP address it stops working. This is only happening on safari, I’m assuming that the IOS simulator is using safari and that’s the reason it’s not working on the Simulator

The question is, what is the difference between safari and other browsers in regards to CORS? Again here is the list of where it works and doesn’t work

This works on:
IOS, actual devices
Android, actual devices and emulators
chrome, ionic serve

Doesn’t work on:
IOS Simulator
safari, ionic serve

install whitelist node module

"cordova-plugin-whitelist": "^1.3.3"

It’s installed, still doesn’t work on Safari

I put the following and even that doesn’t work

    <access origin="*" />
    <allow-intent href="*" />
    <allow-navigation href="*" />

then go through this link

check which u r using currently wkwebview or uiwebview. then switch and check.

best option is allow cors access from server to all domains.

I have allowed CORS access in the server. That’s why it works on every other browser /device except for Safari. Even in safari login works, it’s just that subsequent calls are failing. I know it has something to do with CORS but it’s not as simple as that

Unfortunately I had to roll back the changes I put in place to use wkwebview and now we are back to using uiwebview. It’s really frustrating that I have to enable CORS on the server for this, as CORS was put in place for security. Plus add to that the issue I’m having when CORS is enabled that it’s not working for Safari.

This is one more reason I have a very hard time leaning towards Ionic in the Ionic vs React Native debate. If I have to write a new app I would probably start with React even though I love Ionic components and I do like Angular vs React

just want to point out that uiwebview will be deprecated in iOS v12

Great! At least now I know how much time I have to migrate to react native :weary: