gpt4 book ai didi

android - Nativescript在android和ios中访问localhost访问

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:23:03 25 4
gpt4 key购买 nike

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

我的问题是,当从 android 访问本地主机时,我们应该使用 10.0.2.2。在使用 ios 时,我们可以使用 localhost 。

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

最佳答案

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

/*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";
}

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

require("file.js");

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

file.android.js 
file.ios.js

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

关于android - Nativescript在android和ios中访问localhost访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39107770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com