HTTP request issue on IOS : Always return null

#1

I have some problems with $http.get calls in ionic 1 app. This is the code i have :

$http.get('my.api.com', { timeout: 30000 }).then(function(response) {
console.log("fresh", JSON.stringify(response))
}, function(error) {
console.log(error); 
})

I’ve added this in my index.html :

<meta http-equiv="Content-Security-Policy" content="script-src * 'unsafe-inline' 'unsafe-eval'">

and this to my config.xml file:

<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
    <allow-intent href="market:*" />
</platform>
<platform name="ios">
    <allow-intent href="itms:*" />
    <allow-intent href="itms-apps:*" />
</platform>

This call works well on Android (device and emulator), work on all browsers (chrome, firefox and safari), but return null in iOS device. What can be the issue?
Note: my.api.com points to a self made jax-rs 2.0 api and there is no error. I have also added cordova whitelist plugin
I have a high value of timeout because i wanted to see if the request was timed out before the response was present
Thank you !!!

#2

You might need to add some settings to the IOS app .plist. By default iOS (since iOS 9) can now only communicate via https due to Application Transport Security. In the future it will be mandatory for all apps in store.

#3

What am i supposed to add ?

#4

https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33

#5

Doesn’t work. The request ends with status 0 data null

#6

do you get any console errors on xcode console?

#7

No error in console… it returns an object with status:0 and data:null.

#8

I suggest a dumb idea, because I had that a lot while coding this week, async issue?

I never used ionic v1, but you can try to define the value of #item in template view to null or ‘’ in the controller, before the template try to access/change it?

page.ts

market:  any = '';

// .. before the

constructor{
things..
}
#9

this is the log i’m getting:

Object
config: Object
headers: {Accept: "application/json, text/plain, */*"}
method: "GET"
timeout: 25000
transformRequest: [function] (1)
transformResponse: [function] (1)
url: "my.api.com"
Prototype Object
data: null
headers: function(name)
status: 0
statusText: ""
Prototype Object
#10

What do you have as input from API if you do a map=>res(json) or stg like that? Looks like you get an object that is not “unwrapped”. And if you get an “unfolded/unwrapped” object, it will forever display “undefined” in template.

#11

Are you sure the API URL is reachable in the network

#12

This is the code i have in the service (called provider in V2)

$http.get('my.api.com', { timeout: 30000 }).then(function(response) {
console.log("fresh", JSON.stringify(response))
}, function(error) {
console.log(error); 
})

And the line console.log(error) produces this object as error:

Object
config: Object
headers: {Accept: "application/json, text/plain, */*"}
method: "GET"
timeout: 25000
transformRequest: [function] (1)
transformResponse: [function] (1)
url: "my.api.com"
Prototype Object
data: null
headers: function(name)
status: 0
statusText: ""
Prototype Object

That same code works on Android (Simulator and device perfectly), even in browser.

As input from my API I have:

serviceFactory.getPdvs().then(function(markers)

where service factory is the provider.

#13

Yes. As it prints the error i copy the url and test in safari browser, it works well, i have the same project in an Android device, and it passes well, I have the good result. But in IOS it doesn’t work.

#14

Nothing to do with a CORS issue specific to Safari? No HTTP error code like forbidden? (403) Is your app in production and validated on one app store not the other?

#15

No, it prints the good result in the browser. I’ve read about ATS security applied in ios10 which can be the source of this behavior and I don’t know how to apply to my case as i have a public IP address with port.

#16

Ah! I read something about that for ionic native and ios9/10 fix… this was on the forum several monthes ago, but which topic no idea sorry :confused: If I remember, it said yeah that since ios9/10 CORS has been reinforced, dig a bit maybe.

#17

Thank you for your help, i will continue research

#18

CORS issue should not happen on Device.
Add

 <allow-navigation href="*" />

on your config.xml and see if its working.

Note: Make sure cordova whitelist plugin is installed.

Ionic 3 Http get request - null response iOS only
#19

cordova whitelist works only on Android.

#20

To solve this issue i finally installed a certificate to have https request. Everything works well now.