Calling a phone number in swift
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Calling a phone number from an iOS app does not mean your app places the call itself. What you actually do is ask the system to open a tel URL, which hands control to the Phone app if the device supports calling.
The Basic tel URL Flow
The usual steps are:
- sanitize the phone number
- build a
telURL - check whether the device can open it
- ask
UIApplicationto open it
A small example:
This works on real iPhone hardware. The simulator cannot actually place phone calls.
Why Sanitizing the Number Matters
Phone numbers often arrive in display-friendly formats such as:
- '
+1 (555) 123-4567' - '
555-123-4567' - '
555 123 4567'
Those are fine for display, but the dialing target should be normalized before creating the URL. A simple sanitization pass removes spaces and punctuation so the tel URL is predictable.
For serious international support, you should ideally store or derive a canonical dialable number rather than trusting ad hoc formatting from user input.
A Reusable Helper Pattern
In larger apps, it is cleaner to keep calling logic in a small helper instead of scattering UIApplication.shared.open across view controllers.
That keeps your UI code simpler and makes testing easier if you later abstract the system dependency further.
Add a Confirmation Step for Better UX
Calling is a high-impact action, so many apps confirm before dialing.
This prevents accidental taps from immediately triggering a phone action.
Real-Device and Platform Considerations
Even correct code can appear broken if you test in the wrong environment.
Important constraints:
- the iOS simulator cannot place calls
- iPads and some devices may not support telephony directly
- the URL may be valid while the device still cannot complete a call
That is why canOpenURL matters and why testing on real hardware is important for this feature.
tel Versus Old Alternatives
Older discussions sometimes mention telprompt. In modern practice, it is safer to use tel and handle confirmation in your own UI. That keeps the flow predictable and avoids relying on legacy behavior patterns.
Common Pitfalls
The most common mistake is testing only in the simulator and assuming the code is broken because nothing dials. Phone calling requires real device support.
Another issue is building the URL from an unsanitized display string full of spaces or punctuation. Clean the number first.
Developers also sometimes skip canOpenURL and go straight to open, which makes failure handling less clear on unsupported devices.
Finally, do not surprise users with an immediate call on a single tap unless the app context makes that behavior obviously intentional.
Summary
- In Swift, calling a number is done by opening a
telURL throughUIApplication. - Sanitize the number before building the URL.
- Check
canOpenURLto handle unsupported devices cleanly. - Prefer an app-level confirmation step for safer UX.
- Test real dialing behavior on actual hardware, not only in the simulator.

