简体   繁体   English

如何在 iOS 上连接到真实设备上的本地主机?

[英]How to connect to localhost on a real device in react native on iOS?

I have a server running on localhost:5000 on my desktop, and when I make http requests through my react-native app when running on the iOS simulation to it, it works.我的桌面上有一台服务器在 localhost:5000 上运行,当我在 iOS 模拟上运行时,通过我的 react-native 应用程序发出 http 请求时,它可以工作。 However, when I connect to a real device, the http requests don't work.但是,当我连接到真实设备时,http 请求不起作用。 What am I doing wrong?我究竟做错了什么? Is there something I have to configure?有什么我必须配置的吗?

localhost in your desktop is not the same ip of the localhost of your device.您桌面中的 localhost 与您设备的 localhost 的 IP 不同。 Get the ip of your computer by typing ifconfig in a terminal, then replace localhost by this ip, you should type something like that in your device browser:通过在终端中输入 ifconfig 来获取您计算机的 ip,然后用这个 ip 替换 localhost,您应该在您的设备浏览器中输入类似的内容:

192.168.0.23:5000 192.168.0.23:5000

Maybe my answer is too late but I ran into a similar situation today and I got it to work by running my node server using the IPv4 of my desktop and then making the request to that IP.也许我的回答为时已晚,但我今天遇到了类似的情况,我通过使用我桌面的 IPv4 运行我的节点服务器然后向那个 IP 发出请求来让它工作。

For example, I am running my nodejs development server at http://192.168.0.135:3000 (local IP of my desktop) instead of localhost and then I make the fetch API request from my react-native app to http://192.168.0.135:3000/ . For example, I am running my nodejs development server at http://192.168.0.135:3000 (local IP of my desktop) instead of localhost and then I make the fetch API request from my react-native app to http://192.168.0.135:3000/ .

Just make sure your nodejs server and your app are on the same local network.只要确保你的 nodejs 服务器和你的应用程序在同一个本地网络上。 My react-native server (metro bundler) is running on a separate PC also in the same wifi network.我的 react-native 服务器(metro bundler)也在同一 wifi 网络中的另一台 PC 上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM