我有一个本地节点休息API在我的系统上运行。Nativescript在android和ios上访问本地主机访问
我的问题是,当从android访问本地主机时,我们应该使用10.0.2.2。而当使用ios时,我们可以使用localhost。
那么,有没有办法,我们可以控制我们打电话,取决于环境iOS或Android在nativescript应用程序的主机名
我有一个本地节点休息API在我的系统上运行。Nativescript在android和ios上访问本地主机访问
我的问题是,当从android访问本地主机时,我们应该使用10.0.2.2。而当使用ios时,我们可以使用localhost。
那么,有没有办法,我们可以控制我们打电话,取决于环境iOS或Android在nativescript应用程序的主机名
内JS文件,你可以使用以下(更好当u需要快速决定使用什么每个平台的代码)
/*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";
}
,或者如果u需要基于平台的更复杂的Android/IOS代码,你可以写两个文件,一个用于Android系统,一个用于与名称约定iOS和要求如下行
require("file.js");
你会根据当前运行平台运行时,当u创建具有下列名称
file.android.js file.ios.js
http://docs.nativescript.org/ui/supporting-multiple-screens.html#platform-qualifiers
添加答案
的打字稿版本两个文件获取文件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";
}