Nativescript在android和ios中访问localhost访问 [英] Nativescript accessing localhost access in android and ios

查看:25
本文介绍了Nativescript在android和ios中访问localhost访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统上运行着一个本地节点 rest api.

我的问题是,当从 android 访问 localhost 时,我们应该使用 10.0.2.2.使用 ios 时,我们可以使用 localhost .

那么有没有一种方法可以控制我们调用的主机名,这取决于原生脚本应用程序中的ios或android环境

解决方案

在 JS 文件中你可以使用以下内容(当你需要快速决定每个平台代码使用什么时更好)

/*在顶层文件*/var platform = require("平台");var nativePlatformLocalhost;/*在某些函数或全局*/if(platform.device.os === platform.platformNames.ios){/* ios 本地主机 */nativePlatformLocalhost="localhost";}else if(platform.device.os === platform.platformNames.android){/*安卓本地主机*/nativePlatformLocalhost="10.0.2.2";}

或者如果你需要更复杂的基于平台的 android/ios 代码,你可以编写两个文件,一个用于 android,一个用于具有命名约定的 ios,并且需要如下行

<块引用>

require("file.js");

当您创建两个具有以下名称的文件时,您将获得基于当前运行平台运行时的文件

<块引用>

file.android.js文件.ios.js

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

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

解决方案

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

这篇关于Nativescript在android和ios中访问localhost访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆