Search code examples
iosapicordovamobilephonegap

Can't fetch API with cordova on iOS physical device


It seems my Cordova app can't access my API... It always gives me an error (with fetch() for instance: "TypeError: Load failed").

I guess this is some specific config thing that I haven't found yet.

I know my Django API works and is accessible from my mobile device (using API Tester for example).
I have followed the steps to allow CORS (CORS_ALLOW_ALL_ORIGINS = True, in settings.py)
Fetching from a swift app works on the same phone.
Fetching a public API doesn't work either with Cordova.
Fetching with Cordova works on my iOS simulator.
I have tried some config modifications in the plist file or config.xml.

I would be glad if someone could help me, thank you.
I can provide any additional information one might need to help my case.


Solution

  • Here is a solution that worked for me, for anyone that might encounter this problem one day :D

    Use the Cordova plugin : cordova-plugin-advanced-http

    It provides a native implementation to perform the request, instead of using WebView, which was blocking the request, from what I understand...

    The plugin offers the global object "cordova.plugin.http" that you can use to perform the request (check GitHub repo for more information on how to implement it).