neptunes
neptunes

Reputation: 41

I cannot access my localhost from my iphone

Basically I wanna access the server running on my MacBooks localhost from my iPhone in order to fully test my flutter application. I am on the same network but I cannot access it through my MacBooks IP address. I tried shutting down the firewall but that didn't work either. I then tried to connect my iPhone through USB and enable it via "internet sharing" from my Mac but it still did not work...

Upvotes: 3

Views: 7505

Answers (5)

Arjun Patel
Arjun Patel

Reputation: 109

For anyone reading this in 2025, the real answer is adjusting your Info.plist with `NSAppTransportSecurity` as described here. You can read and see the additional option for NSAllowsLocalNetworking.

Upvotes: 0

TSR
TSR

Reputation: 20637

It's very easy. At least for me.

  1. Connect the iPhone Via USB (This does not work on wireless debugging)

  2. Go to Settings > Search Sharing and at the bottom, there is your Mac Local host name. yourname-MacBook-Air.local

  3. Test it on the iPhone Safari, go to yourname-MacBook-Air.local:port replace port with your server port. Mine was 8080

It works:

  • no need to enable hotspot on either on the devices,
  • no need to configure any proxy,
  • no need to configure internet sharing whatsoever
  • No need to have internet at all on both the device. This works offline
  • No need to be on the same wifi network

On Android, I use ADB reverse tcp.

enter image description here

Upvotes: 9

Aayush Rawat
Aayush Rawat

Reputation: 109

I was trying to run my react (vite) website on my phone, and both of my devices were on the same network, even then I was not able to run the website on my mobile device. Apparently, while using Vite, you have to bind your server to a specific network instance, for that make these changes to your package.json

   "scripts": {    
        "dev": "vite --host",  //Modify this
        "build": "vite build",
        "preview": "vite preview"
      },

If you are using webpack or any other bundler, just see the terminal after running the server, it will give you the instruction to host it. Hope it helps

Upvotes: 4

tyirvine
tyirvine

Reputation: 2391

If you're using a macOS device to run your local server, try the following:

  1. Get your IP address by holding down ⌥ option and clicking on the Wi-Fi icon in your status bar. If you're connected through ethernet go to System Settings → Network → (Your Ethernet Device) to view your IP address.

  2. If you have a firewall enabled, disable it. Then restart your machine to ensure the changes have taken affect. (This was what got me).

  3. Make sure both the iOS and macOS device are on the same network, start your local server, and then try connecting.

  4. Then if you want, you can re-enable your firewall. Then click Options... and ensure that the applications (Terminal, Visual Studio Code, and so on) you're using to run your local server can accept incoming connections. Restart to verify your changes have taken affect.

Upvotes: 1

Canilho
Canilho

Reputation: 1209

Localhost It's not an external IP.
Localhost Cannot be accessed from other machines. localhost refers to the internal address of the machine that is running the server.

if you want to know the IP address of your MacBook, you must look up your IP in your network.

Open a terminal and write ifconfig to know what is your local IP address.

You might have several IPs there, just make sure your phone is connected to the same Network as your Mac, or just use your MacBook as a Hotspot, and check what's the IP of the machine.


Make sure that the server you are running is not being blocked by any Firewall

Upvotes: 1

Related Questions