从 XCode 访问 Mac 上的本地主机?Phonegap 与 Ajax 通信到本地 Rails 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5776412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access localhost on Mac from XCode? Phonegap communicating with Ajax to a local Rails app
提问by Lance Pollard
Is there a way to access http://localhost:3000/posts
from within an HTML file that's running through Phonegap for the iPhone on the iPhone device (not the simulator), in XCode?
有没有办法http://localhost:3000/posts
在 XCode 中从 iPhone 设备(不是模拟器)上的 iPhone 的 Phonegap 运行的 HTML 文件中进行访问?
If I have an HTML5 app in Phonegap, I have only been able to access external stylesheets with file://Users/etc..
or http://...
when I'm testing it on the iPhone/iPad itself. If I'm running the simulator, I can access localhost no problem.
如果我在 Phonegap 中有一个 HTML5 应用程序,我只能使用file://Users/etc..
或http://...
在我在 iPhone/iPad 本身上测试它时访问外部样式表。如果我正在运行模拟器,则可以访问 localhost 没问题。
Is there a way around this? I know I can access localhost on the Mac from within Parallels running Windows by doing http://username.local/posts
, is there something like this for iOS development?
有没有解决的办法?我知道我可以从运行 Windows 的 Parallels 中访问 Mac 上的 localhost http://username.local/posts
,iOS 开发有没有类似的东西?
回答by Andreas Ley
"localhost" always points to 127.0.0.1
in IPv4, which is the current device's loopback network interface. If you open "localhost" from your iPhone, it will point to the iPhone itself.
“localhost”始终指向127.0.0.1
IPv4,即当前设备的环回网络接口。如果您从 iPhone 打开“localhost”,它将指向 iPhone 本身。
However, if you are in the same network as your Mac, you can simply use your Mac's IP address, e.g. http://192.168.0.20:3000/posts
. You can see your current IP address in your Mac's Network Settings.
但是,如果您与 Mac 位于同一网络中,则只需使用 Mac 的 IP 地址,例如http://192.168.0.20:3000/posts
. 您可以在 Mac 的网络设置中查看您当前的 IP 地址。
Also, depending on your network configuration, using the Bonjour name might work as well. For example: http://mymac.local:3000/posts
if the name of your computer is set to mymac
in the system preferences' Sharing
pane.
此外,根据您的网络配置,使用 Bonjour 名称也可能有效。例如:http://mymac.local:3000/posts
如果您的计算机名称mymac
在系统首选项的Sharing
面板中设置为。
回答by Tony Lukasavage
If you are asking whether you can access localhost on your Mac/PC from your iPhone device, then the answer is no. There's no way for your iPhone to route to another machine's local host address.
如果您问是否可以从 iPhone 设备访问 Mac/PC 上的 localhost,那么答案是否定的。您的 iPhone 无法路由到另一台机器的本地主机地址。
You should get your iPhone on the network that the server resides on and use an accessible IP address to access it.
您应该将 iPhone 连接到服务器所在的网络,并使用可访问的 IP 地址来访问它。