Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How do I make a button that calls a phone number in Xcode and Swift?

I need to make a button that calls a phone number in Xcode. Please help!

2 Answers

Alphonso Sensley II
Alphonso Sensley II
8,514 Points

@aidenmitchell Im not an expert in this area, but this tutorial may help you https://www.raywenderlich.com/150015/callkit-tutorial-ios

Michael Hulet
Michael Hulet
47,913 Points

I know this is an old question, but I just stumbled across it on the internet, so I thought I'd give it an answer if you're still looking, or for anyone else that stumbles across it, too

There isn't really a standard API for making a phone call, but you can trigger it by making and using the proper URL. Specifically, telprompt or tel should work as a scheme, and then it should be followed by the phone number you want to call (just the numbers, though). For example, this should work:

var components = URLComponents()
components.scheme = "telprompt"
components.host = "15551234567"

guard let phoneURL = components.url, UIApplication.shared.canOpenURL(phoneURL) else {
    fatalError("Don't actually fatalError here, do something that makes sense in the context of the rest of your code")
}

UIApplication.shared.open(phoneURL)