Phones and mobile apps2 minutes to read

Why does my app’s API call work in the simulator but fail on a real phone?

Fix “Network request failed” and “Connection refused”: localhost on a phone is the phone itself, and iOS and Android block plain HTTP by default.

The phone can’t reach localhost, and it blocks plain HTTP

Two things usually cause it. On a real phone, localhost means the phone itself, not your computer. And release builds on iOS and Android refuse plain http:// by default. The simulator hides both problems. The fix that lasts is to deploy the API to a host with an HTTPS address and point the app at that URL.

What each device means by localhost

Where a request to localhost goes
Running onlocalhost reachesTo reach your computer
iOS SimulatorYour Maclocalhost works
Android EmulatorThe emulator10.0.2.2
Physical phone on Wi-FiThe phoneYour computer’s LAN IP, while on the same network
Physical phone on mobile dataThe phoneNot reachable. Deploy the API

Why plain HTTP fails

iOS App Transport Security blocks non-HTTPS connections unless the app declares an exception. Android blocks cleartext traffic by default for apps targeting Android 9 (API 28) or later. Development builds sometimes allow it, so the bug can appear only in a release build or TestFlight. You can add exceptions, but stores review them and they leave users’ traffic unencrypted. Use HTTPS instead.

Give the API a real HTTPS address

Deploy the API to Bothosting as an HTTP app, bound to 0.0.0.0 on SERVER_PORT. Copy its HTTPS address and request a known route from your phone’s browser first. If the browser gets the JSON, the app will too once its base URL points there.

Point the app at the deployed API
// config.ts
export const API_URL = __DEV__
  ? 'http://10.0.2.2:3000' // Android emulator only
  : 'https://api.example.com';

If the browser also fails, the server isn’t listening where the platform expects. See an API that is listening but unreachable and hosting a mobile app’s backend.