How to Make Phone Calls in Swift

Calling a phone number in swift

Just try:

if let url = NSURL(string: "tel://\(busPhone)") where UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.sharedApplication().openURL(url)
}

assuming that the phone number is in busPhone.

NSURL's init(string:) returns an Optional, so by using if let we make sure that url is a NSURL (and not a NSURL? as returned by the init).


For Swift 3:

if let url = URL(string: "tel://\(busPhone)"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}

We need to check whether we're on iOS 10 or later because:

'openURL' was deprecated in iOS 10.0

How to make a call in ios swift 5 using UITextview?

TextView provide one delegate method to get click event of any detector like phone number, link.

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if URL.scheme == "tel" {
let phone = URL.absoluteString.replacingOccurrences(of: "tel:", with: "")

if let callUrl = URL(string: "tel://\(phone)"), UIApplication.shared.canOpenURL(callUrl) {
UIApplication.shared.open(callUrl)
}
}
return true
}

Also don't forgot this.

textView.dataDetectorTypes = [.phoneNumber]
textView.delegate = self

Given a phone number, how do I make a phone call in iOS?

Thanks to Leo Dabus,

I simply had to add the "tel://" aspect to each phone number. In this project, I decided to add this code snippet into my function rather than my CloudKit records.

if let call = detail.value(forKey: "Phone") as? String,
let url = URL(string: "tel://\(call)"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
}

Open phone app with auto populated number in Swift

Its not possible to open direct phone app without dialogue. As Per Apple's documentation for openURL:

openURL

When a third party application invokes openURL: on a tel://,
facetime://, or facetime-audio:// URL, iOS displays a prompt and
requires user confirmation before dialing.

How to use openURL for making a phone call in Swift?

I am pretty sure you want:

UIApplication.sharedApplication().openURL(NSURL(string: "tel://9809088798")!)

(note that in your question text, you put tel//:, not tel://).

NSURL's string init expects a well-formed URL. It will not turn a bunch of numbers into a telephone number. You sometimes see phone-number detection in UIWebView, but that's being done at a higher level than NSURL.

EDIT: Added ! per comment below



Related Topics



Leave a reply



Submit