Reputation: 11140
I'm trying to ask for phone call programmatically but I'm not able to construct URL from my nine-digit phone number. When I try it with for example 999999999 phone number, it works, it asks for call
@IBAction func callButtonPressed(_ sender: UIButton) {
askForCall(to: "999999999")
}
func askForCall(to number: String) {
guard let url = URL(string: "tel:\(number)"), UIApplication.shared.canOpenURL(url) else { return }
UIApplication.shared.open(url)
}
but when I use real phone number 736XXXXXX it shows nothing.
Note: when I try it without canOpenUrl it doesn’t work so I guess problem is with constructing URL from my real number
Any ideas?
Upvotes: 1
Views: 655
Reputation: 349
You should type "tel://" + number
and not tel:\(number)
EDIT 2
Try something like this
func call(phoneNumber: String) {
if let url = URL(string: phoneNumber) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(phoneNumber): \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(phoneNumber): \(success)")
}
}
}
let number = "736XXXXXX"
let phoneNumber = "tel://\(number)"
call(phoneNumber: phoneNumber)
Try with that number to see if it's a bigger problem than the simple code :)
Upvotes: 2
Reputation: 56
You need to add the scheme 'tel' into your info.plist
<key>LSApplicationQueriesSchemes</key>
<string>tel</string>
Then normal use:
guard let url = URL(string: "tel://\(phoneNumber)"), UIApplication.shared.canOpenURL(url) else {return}
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
Goodluck
Upvotes: 0