How to get my IP address programmatically on iOS/macOS?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
On iOS and macOS, “my IP address” can mean two different things: the local address assigned to a network interface such as Wi‑Fi, or the public address seen by internet services. Those are retrieved in different ways. Local addresses come from system interfaces, while public addresses usually require asking an external service.
Decide Whether You Need Local or Public IP
The first question is what your app actually needs.
- local IP is useful for devices on the same LAN
- public IP is useful for diagnostics or internet-facing networking
These are not interchangeable. A phone on Wi‑Fi might have a local address like 192.168.x.x, while its public address is the one exposed by the router or carrier network.
Read Local Interface Addresses with getifaddrs
On Apple platforms, the standard low-level way to enumerate interface addresses is getifaddrs. You inspect each interface, filter for address families such as IPv4 and IPv6, and then choose the interface names you care about.
This returns a dictionary keyed by interface name such as en0 or pdp_ip0.
Pick the Interface You Actually Want
On Apple devices, several interfaces may exist at once. Typical examples include:
- '
en0for Wi‑Fi on many devices' - '
pdp_ip0for cellular data on iPhone' - '
lo0for loopback'
A helper that prefers Wi‑Fi and then cellular is often practical:
This is usually better than returning the first interface blindly, because the first one may be loopback or an interface you do not actually want.
Public IP Requires an External Service
Your app cannot infer its public internet address purely from local interfaces in the general case. NAT and carrier networks mean the public address is often owned by a gateway, not by the device interface directly.
The normal solution is to query a service that returns the caller’s public IP.
You can call it from an async context:
This is the simplest and most reliable public-IP approach for most apps.
IPv4 vs IPv6
Apple platforms fully support IPv6, and many networks now prefer it. That means your local-IP logic should not assume IPv4 only unless the app explicitly requires it.
If you only want IPv4, filter for AF_INET. If you want to be network-agnostic, keep both AF_INET and AF_INET6 and decide later which one is appropriate for the use case.
Common Pitfalls
The most common mistake is asking for “the IP address” without deciding whether local or public IP is required. Another is returning the first interface found, which often produces loopback or an irrelevant address instead of Wi‑Fi or cellular. Developers also sometimes try to derive the public address from device interfaces, which does not work reliably behind NAT. A final issue is ignoring IPv6 and hardcoding IPv4-only assumptions into code that will run on modern Apple networks.
Summary
- Local and public IP addresses are different and require different techniques.
- Use
getifaddrsto enumerate local interface addresses on iOS and macOS. - Prefer a specific interface such as
en0orpdp_ip0rather than taking the first result blindly. - Public IP usually requires an external service queried through
URLSession. - Keep IPv6 in mind unless the use case is explicitly IPv4-only.
Related reading
- How to get Python requests to trust a self signed SSL certificate?
- How to get running pod status via Rest API
- How to get self pod with kubernetes client-go
- How to get status code from webclient?
- How to get RGB values from UIColor?
- How to get root view controller?
- How to get the cpu usage per thread on windows win32
- How to get the IP address of the docker host from inside a docker container

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.