简体   繁体   中英

Can't connect to local rails API from iOS native app

I have an iOS native app.

I have an API developed in Rails (4.1.4), which lives at : http://api.myapp.local:5000 .

I have configured my /etc/hosts to do my basic API / web development, no problem everything work perfectly.

My problem is that I want to consume my local API through my iOS app (in an actual device, not in simulator) over WiFi.

Here's what I've tested (that does not work)

  • Using xip.io with my local en0 inet ip with thin webserver.
  • Using xip.io with my local en0 inet ip with pow webserver.
  • I've done some port forwarding to connect via my public IP

The problem is essentially due to the subdomain, indeed: all above solutions work if my app doesn't have any subdomain. (I can reach my local API this way, but this is not what I want)

My issue is that I can't tell my iOS app, "connect to this IP, using this subdomain"

I probably have a lack of knowledge in term of network, but I'm really annoyed by this problem, since I really need to test my local webserver with my iOS app.

Here is what my routes file looks like:

constraints subdomain: 'api', format: 'json' do
  scope module: :api do
    scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
      resources :packs, only: [:index, :show]
    end
  end
end

Since you're testing on a device, you do not have any networking access to your computer, which is hosting the server.

You may use an application like Charles to setup a proxy. There is WireShark as well. Upon setting either up, you would point your device to your computer via wifi settings on the phone.

Both options take some time & basic networking understanding to setup.

Sooo...

You're alternative to that would be to test from the iOS simulator. I highly advise this route because this requires no setup, just point your requests to http://api.myapp.local:5000 & you're good to go.

The last option you have would be to setup a "staging" server and point your requests to that domain and then compile the app to the device.

In order to access your local API server, you need to connect to the "local" IP address (or hostname) of the computer running the API on the same network. So if the computer and the iPhone are on the same WiFi network, they will be able to communicate.

To get your local IP address, you can use ifconfig :

$ ifconfig en0 | grep "inet "
    inet 10.0.0.14 netmask 0xffffff00 broadcast 10.0.0.255

In this case, my local IP is 10.0.0.14. Now your iOS app can make an HTTP request to http://10.0.0.14:5000 . However, I find it easier to use hostname :

$ hostname
Andrews-MacBook-Pro.local

Now your iOS app can make an HTTP request to http://Andrews-MacBook-Pro.local:5000 .

However, since your Rails app routes the API requests based on an API subdomain, you won't be able to use the IP address alone.

One option is to specify the host as an HTTP header when your iOS App makes the request. Here's an example using curl:

curl -H "Host: api.myapp.local" http://10.0.0.1:5000

However, depending on how you've implemented your API requests throughout your codebase, it may be a lot of work to specify this HTTP header everywhere. If you can't do that, then your only other option, since you can't modify the iPhone's "/etc/hosts" file, is to create a publicly accessible domain which points to this local IP address. Here's what you need to do:

  1. Register your domain. Something like benjiox-dev.com
  2. Configure your domain's DNS with an "api" subdomain with either:
    • an A record that points to the local IP address of your computer.
    • a CNAME record that points to the local hostname of your computer.
  3. Configure you iOS App to use your new domain http://api.benjiox-dev.com:5000

Note that your IP address may change on your internal network, so you may have to update your A record when it changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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