1

So i'm building an Ionic App with API calls, everything works fine when i run it on the browser and i get all my data.

But when i build the app (ionic cordova build android) and install the .apk in my phone, i get NO data, the view is just empty and i don't know why.

I followed instructions that i found here but still nothing.

I even installed "android-permissions" cordova plugin as explained in this question but when i'm installling the app one my phone, it says that the app does not require any permission.

I'm lost and don't really know why it is not working. Help neede please !.

Information about the project

Created using "ionic start appName blank --type=angular"

Ionic Framework :

@ionic/angular 4.9.1

Ionic CLI :

5.4.1

Cordova CLI :

9.0.0

Cordova Platforms : 

android 8.1.0

Thank you

Tony Stark
  • 43
  • 1
  • 9

1 Answers1

1

This could be a whitelisting or, if you have Android 9 and your API does not use TLS, a network security issue.

Whitelisting

Add the tag <access origin="https://your-server.com" /> to your config.xml.

Network Security

To allow Android 9 devices to access http:// (non-TLS) APIs, additional configuration is required. In a Cordova app, perform the following steps:

  1. Create a file called network_security_config.xml in your project root containing the following:
    <?xml version="1.0" encoding="utf-8"?>
      <network-security-config>
        <domain-config cleartextTrafficPermitted="false">
      </domain-config>
    </network-security-config>
  1. Add this to your config.xml to include the new configuration in your Android Manifest:
    <platform name="android">
        ...
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
            <application android:networkSecurityConfig="@xml/network_security_config" />
        </edit-config>
        <resource-file src="network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
        ...
    </platform>

Rebuild the Android platform (i.e. ionic cordova platform rm android and ionic cordova platform add android etc.) and it should work from your device.

Alex Steinberg
  • 1,426
  • 13
  • 25
  • Alright thank you, i'm going to try it right now and come back – Tony Stark Sep 25 '19 at 13:52
  • Does your endpoint use SSL? And does your request complete with a 200 response but there is not payload, or does the request error out? – Alex Steinberg Sep 26 '19 at 07:49
  • No it does not use SSL. And the request responses with a 200 code. Everything works fine on the browser/computer. The problem is the Android device that i install the app on, i'm seeing no data. And i don't know if it is a CORS issue or something with the Android permissions (internet, network_state...) – Tony Stark Sep 26 '19 at 10:42
  • Are you on Android 9? They require you to use SSL. Have a look at this question: https://stackoverflow.com/questions/51902629/how-to-allow-all-network-connection-types-http-and-https-in-android-9-pie – Alex Steinberg Sep 26 '19 at 10:56
  • Yes i'm on Android 9. Okay i'm going to take a look at the question and come back to you. Thanks – Tony Stark Sep 26 '19 at 11:06
  • I updated the answer with instructions on adding the necessary config to an Ionic/Cordova app. – Alex Steinberg Sep 26 '19 at 11:27
  • Hey, thanks to you i now know what was the issue. It's exactly what you said, the network security configuration with Android 9 because i tested the app on another phone runnig Android 5 and it's working just fine. But now after editing the project with the additional configuration that you just provided, the app is not working anymore on the Android 9 device, it starts then stops right away – Tony Stark Sep 26 '19 at 12:17
  • What i did is that i did not create a new **network_security_config.xml** file, i found that it was existing on my project and it was referenced it the _config.xml_ , so i just edited it. Is that wrong? – Tony Stark Sep 26 '19 at 12:19
  • The file was looking like this ` localhost ` – Tony Stark Sep 26 '19 at 12:19
  • Hey @alex , it's finally working. I finally fixed it.I just had to edit the file adding this ` ` – Tony Stark Sep 26 '19 at 13:52
  • Thanks, you helped me a lot – Tony Stark Sep 26 '19 at 13:52
  • @tonyStark hi I'm having the same issue.. can you please help me saying specifically what you did to solve the problem? – Prabhashi Buddhima Nov 17 '19 at 07:26
  • 1
    Hey @kavindhi , i'll be glad to help. I'm a little bit limited in the comments so i made a file that explains the issue and how to solve it. Here is the [link](https://drive.google.com/open?id=1gco9J6su8lBgyF3wVt6lgDj8mD-DzY3A). I stay tuned for more questions – Tony Stark Nov 18 '19 at 12:33
  • @kavindhi You're welcome, have your issue been fixed ? – Tony Stark Nov 20 '19 at 11:56
  • @tonyStark yes. my problem happens due to iframe tag – Prabhashi Buddhima Nov 22 '19 at 05:40
  • @kavindhi Aight , glad to help – Tony Stark Nov 23 '19 at 06:02