Nativescript accessing localhost access in android and ios

落爺英雄遲暮 提交于 2019-11-29 11:59:41

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

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";
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!