How to make a request to localhost via iOS device (using Xcode)? - ios

I have a localhost website up at localhost:1336. When I run it on my simulator using Xcode, the app does not encounter any problem make a request to the localhost for data. But when I connect my iPhone via USB cable to the mac, and run the app on the iPhone device and makes the request, but then I get an error "Error- Could not connect to the server.".
I also confirmed that they are on the same Wi-Fi and using same IP. On Safari on my iPhone, I went to http://IPNumber:1336 and it was able to access the site via my iPhone. But some reason, through the app, it cannot connect to the server.
Any input or insight on this would be greatly appreciated. Thank you in advance.

the term 'localhost' means the same device, i think you mean a host on your local network? going localhost:1336 means your device is connecting to itself, its the same as going 127.0.0.1:1336. hence why it works on your simulator but not your phone, since your server and simulator are running on the same device
you have to use http://IPNumber:1336 in your app as well

"Localhost" means the same device. If you are not running a HTTP service on your iPhone, you should not use "localhost".
Another point, you are using "http", if your iPhone is running iOS9+, you will need to check whether App Transport Security will be an issue, here is an article on how to do that: http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/

Related

iPhone can not connect to local HTTP server over WiFi

I can not open static HTML pages served from a HTTP server run on my MacBook in iPhone 6s iOS 9.2, neither in 'Safari' nor in 'Chrome' browser when using: http://ipNumberOfMyServer:portNumber
I am able to open the pages on Android phones and I'm also able to open the pages if I turn MacBook's WiFi off and on, but if I change the page and refresh the browser, it again fails to load the page and says that server has stopped responding or that Safari could not connect to the server.
Resetting network settings in iPhone didn't help, nor did turning off Cellular Data in iPhone settings. Any idea what's wrong?
I know this is old already. You can get you computer name from System Preferences -> Sharing
System Preferences->Sharing
in your iPhone safari, type http://JimbasMekbuk.local
if you using different port, say 3000, then add :3000
make sure you turn off firewall
I hope this will help :-)
I had a similar problem, I changed the Wi-Fi network from 5GHz to 2.4GHz and this fixed it.

iPhone connection to localhost rails sarver

I am creating Rails server for iPhone app.
I want to debug on iPhone device not on iPhone simulator.
So, I connect iPhone to mac book air by USB, and connect to Rails server.
But, iPhone could not connect to Rails server.
Environment
Rails 4.2.3
Server localhost:3000
Xcode 6.4
iPhone iOS 8.4.1
en0 inet 192.168.11.7
As long As I searched, when iPhone device connect to localhost server, the url is http://192.168.11.7:3000/~.
But, mac console show nothing and xcode show "The request timed out." after a while.
If anyone know how to fix the problem, please tell me.
Use the wonderful http://xip.io/
You don't need to install anything - just go to
http://192.168.11.7.xip.io:3000
on your phone.
If you want to use a particular domain name, eg if your app behaves differently depending on domain names or subdomains, you can do eg
http://mydomain.foo.192.168.11.7.xip.io:3000
EDIT: my answer is actually about connecting to your server over the internet rather than a usb cable. I'd recommend that you use the internet rather than the USB option if possible as it's simpler. xip.io will not work if you can't access the internet.
You probably tried that but funny enough, in my case turning my iPhone off and on again actually fixed the issue.
I had the same problem, my app was working fine and all of a sudden it stopped working although nothing had changed. I could access the internet, the browser was accessing the rails app url just fine but my iOS app did not seem to be able to connect to it.
I needed to run my iOS app from Xcode with my local rails server to my device and xip.io was the solution.
Get the ip address of your computer from the wireless network you are connected to and put that into networking.swift like so.
http://11.1.11.100.xip.io:3000
Make sure the rails server is running and your device is connected to the same network as your computer and build the app.

How do I get my iphone to connect to local Xampp server on my mac while testing an xcode app

I am doing a Ray Wenderlich tutorial how to make an app like "Instagram". It's my first time setting up a database. I am using Xampp and hosting on my mac.
I CAN get the app to run with the xcode simulator and I can log in and it works with the server. However when I run my iphone device it runs the app but when I go to login through the app it says "Could not connect to server".
What am I missing? I think all my permissions are set to anybody etc... But again I'm totally new to using a database. It seems though that if the simulator can do it the phone should be able to. Also it's not a provisioning problem, that profile is valid and it wouldn't run on my app if it wasn't. I just cant get to the database from the device.
Any help would be awesome!
If the app isn't able to connect to the server, there might be an issue with the server address.
When running on the simulator, you're still on your mac, so something like http://localhost/DATABASE_ALIAS is enough. But when running on a different device, that is no longer true.
To keep things simple, you should make sure the iPhone is connected to same network as your Mac and then replace localhost with either the Mac local IP or its Bonjour address, for instance: http://YOUR_MAC_NAME.local/DATABASE_ALIAS.
To find out the Bonjour address, open System Settings and find the machine name under Sharing. As an example, if it is called "Herbie MacBook", the address would be "Herbie-MacBook.local".

Access localhost on Mac from XCode? Phonegap communicating with Ajax to a local Rails app

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?
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.
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?
"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.
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.
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.
Perhaps you could set up your Mac as a Web Server? See ehow link
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.
You should get your iPhone on the network that the server resides on and use an accessible IP address to access it.

BlackBerry simulator settings for VPN

I want to test a project from the BlackBerry simulator, but our webservice runs on an external server that can only be accessed using the vpn from our PC's.
I can access the webservice from the Android simulator without doing anything special. However I cannot access the webservice from the BlackBerry simulator -- not even from the browser of the simulator -- though I am able to connect to other services on the web from the BlackBerry simulator.
What kind settings are there to fix this?
There's a network setting for the simulator that might be useful for situations like this. You could try manually assigning the vpn ip address you were given.
/ip-address={string}
IP address of the simulated handheld. This should be left blank under normal circumstances. If running on a PC with multiple IP addresses, one can be specified here to cause the simulator to use it.
Cheers
Ray
Ok...anyone bumping on the same problem...
Append ";deviceside=true" to the URL from which data is to be fetched

Resources