Detect if device is iOS
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
“Detect if device is iOS” sounds simple, but the right answer depends on where the code runs. In a native Apple app, the platform is already known at build time. On the web, iOS detection is only a heuristic, and feature detection is usually a better design than strict operating-system sniffing.
In Native Swift Code, Prefer Platform APIs
If you are writing a native app, use compile-time or framework-level checks instead of trying to parse strings.
If you need to distinguish phone from tablet within Apple platforms, use userInterfaceIdiom.
That is more reliable than trying to infer the OS from model names or user-agent text.
On the Web, Avoid OS Sniffing When a Feature Check Will Do
Most web code does not actually need “is this iOS?” It needs something narrower, such as:
- does the device support touch input
- does the browser support a particular API
- does Safari require a platform-specific workaround
If that is the real requirement, check the capability directly.
This is more future-proof than trying to infer every operating system variant.
If You Must Detect iOS in Browser JavaScript
Sometimes a genuine iOS-specific workaround is unavoidable. In that case, use a heuristic and accept that it is still a heuristic.
The MacIntel check exists because some iPads identify themselves more like desktop Safari, which breaks older user-agent tests that only looked for iPad.
Use Detection Only at the Edge
Even when you need an iOS branch, keep that logic small and localized.
That is better than scattering isIOS() checks across the whole codebase. Centralizing the heuristic makes it easier to update when browser behavior changes.
Do Not Use CSS Screen Sizes as OS Detection
Screen-size media queries do not tell you whether a device is running iOS. Many non-iOS devices share similar dimensions, and iOS devices themselves vary widely.
Use CSS media queries for layout, not for platform identification.
Test the Branch You Add
If you add an iOS-specific code path, test the behavior on a real iPhone or iPad and on a non-iOS device. Platform heuristics are easy to get almost right while still missing one browser mode or tablet case.
That is another reason to keep the detection logic centralized. You only need to validate one helper instead of many scattered checks.
Common Pitfalls
The biggest mistake is using operating-system detection when the real requirement is feature detection. That makes code brittle and harder to maintain.
Another issue is relying only on old user-agent checks for iPad, iPhone, or iPod, which can miss newer iPad browser behavior.
A third problem is treating CSS viewport size as proof of the operating system. Layout dimensions are not a reliable platform identifier.
Summary
- In native Swift code, use platform APIs such as
#if os(iOS). - In web code, prefer feature detection over operating-system detection.
- If you must detect iOS in JavaScript, use a heuristic that also considers iPad desktop-style behavior.
- Keep platform checks centralized instead of scattering them across the app.
- Use media queries for layout decisions, not for identifying iOS itself.

