1

So I'm running into what's probably a typical issue and although there seems to be several related questions and answers here and here but they don't exactly apply to my question.

I have a local django server (localhost:8000) running in development. I would like to easily develop a Nativescript mobile app connecting to my django server by using Nativescript/Vue with Axios to make the http calls. I prefer to start development/testing in ios. I have two choices for development with Nativescript - either using 'tns run ios' which brings up the ios emulator on my mac or using 'tns preview' which allows me to test via my actual iphone.

When I am making a basic call via axios to http://localhost:8000/myapi that call yields an http status result of null.

  • I tried using a loopback address (e.g., 10.0.2.2 - which I believe may work for the Android emulator) but that yielded the same result.
  • I tried using my local IP address of my mac - i.e., instead of localhost, using my local 192.168.xx.xx address for the call but that didn't work

  • I finally was able to get it working using ./ngrok to tunnel my local server to an outside IP address. Now that works, but it's not ideal since that IP address may change from time to time.

What is the suggested solution for easily connecting to my local api server using either Nativescript preview or running the ios emulator?

Arthur Frankel
  • 4,695
  • 6
  • 35
  • 56
  • Your iOS Simulator / Android Emulator are standalone virtual devices, so hitting localhost will not work, you must use the local IP address of the machine. By default Http / Clear Text communications are disabled in [iOS](https://stackoverflow.com/questions/56148070/nativescript-localhost-http-call-fails-on-ios) and [Android (v9 and above)](https://stackoverflow.com/a/54018826/1060423), you must enable them. – Manoj Aug 24 '19 at 21:04
  • Yes, thank you - I did know that and had the changes in my Info.plist and as noted in my second bullet I had been trying to connect to my local ip address. I finally figured out my issue. It was not on the Nativescript side but on the django side. For anyone that comes to this page looking for an answer, your server (in this case django server) must be started with the local ip address and not localhost - e.g., python manage.py runserver http://192.168.14.10:8000 (or whatever you local IP address is) otherwise you can't connect to the server regardless of http/https. – Arthur Frankel Aug 24 '19 at 21:38
  • Also to note that this solution does work with both the emulator and the preview on my physical iphone. – Arthur Frankel Aug 24 '19 at 21:40
  • I had a similar question and was able to get it to work for Rails app and testing on an iphone. ngrok definitely works. But I was also able to start my local rails app by specifying the ip address like so: rails s -b [MY_MAC_IP_ADDRESS] -p 3000 and then modifying the request in the app accordingly. – hraynaud Sep 09 '19 at 11:26

2 Answers2

2

Emulator API Path:- http://10.0.2.2:5000. Like Your API Local Path Is http://198.168.1:5000/api or http://localhost:5000/api Then Replace With http://10.0.2.2:5000/api And Port Address Is Same Like As Your Local API Port.

Path- App_resources/Android/src/main/AndroidManifest.xml :-

android:usesCleartextTraffic="true" Add in application tag

iOS File Path- App_Resources/iOS/Info.plist

<key>NSAppTransportSecurity</key>
 <dict>
  <key>NSExceptionDomains</key>
   <dict>
    <key>10.0.2.2:5000</key>
    <dict>
        <key>NSExceptionAllowsInsecureHTTPLoads</key>
        <true/>
    </dict>
  </dict>
</dict>
FrontEnd-er
  • 661
  • 4
  • 13
  • The `usesCleartextTraffic` part of this is what saved me, but I found it in a different answer: https://stackoverflow.com/questions/56815634/why-my-emulator-doesnt-work-with-nativescript – Seph Reed Oct 26 '22 at 23:54
0

As noted by @Manoj you need to allow for http within your emulator settings defined here for ios.

Additionally, if you are using django you must also do the following:

  • determine your local ip address (via ifconfig for mac)

  • Your allowed hosts in settings.py contains this IP address - e.g., ALLOWED_HOSTS = ['192.168.14.14','localhost','127.0.0.1'] - where 192.168.14.14 is your local ip address

  • You start your server as this ip address and NOT your localhost - e.g., python manage.py runserver 192.168.14.14:8000

This will work for both the emulator (tns run ios) and the playground on your phone (tns preview)

Arthur Frankel
  • 4,695
  • 6
  • 35
  • 56