2

I have a local node rest api running on my system.

My question is ,when accessing localhost from android we are supposed to use 10.0.2.2. And When using ios we can use localhost .

So is there a way we can control the host name we call , depending on the environment ios or android in a nativescript app

Rohit Katyal
  • 67
  • 1
  • 10

2 Answers2

4

inside JS file you can use following (better when u need quick decide what to use per platform code)

/*at top file*/
var platform = require("platform");
var nativePlatformLocalhost;

/*in some function or globally*/
if(platform.device.os === platform.platformNames.ios){
  /*localhost for ios*/
  nativePlatformLocalhost= "localhost";
}
else if(platform.device.os === platform.platformNames.android){
  /*localhost for android*/
  nativePlatformLocalhost= "10.0.2.2";
}

or if u need more complex android / ios code based on platform and you can write two files one for android and one for ios with name convention and require as following line

require("file.js");

you will get file based on current running platform runtime, when u create two files with following names

file.android.js 
file.ios.js

http://docs.nativescript.org/ui/supporting-multiple-screens.html#platform-qualifiers

Marek Maszay
  • 1,537
  • 1
  • 9
  • 11
2

Adding the typescript version of the answer

import {platformNames} from "platform";
import {device} from "platform";

  var nativePlatformLocalhost;

     /*in some function or globally*/
    if(device.os === platformNames.ios){
     /*localhost for ios*/
     nativePlatformLocalhost= "localhost";
   }
  else if(device.os === platformNames.android){
    /*localhost for android*/
    nativePlatformLocalhost= "10.0.2.2";
    }
Rohit Katyal
  • 67
  • 1
  • 10