Calling a Phone Number 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

Calling a phone number with Swift

You can declare your phone variable as a global for your class by declaring it at the below of your class declaration as shown below:

class ViewController: UIViewController {

let sharedDefaults = NSUserDefaults.standardUserDefaults()
var phone = ""

}

By declaring this way you can access it anywhere in your class and now you can assign value to it this way in your function:

func base() {

var myBase = sharedDefaults.objectForKey("base") as? String

if myBase != nil {
if myBase == "FRA" {

titleLabel.text = "FRANKFURT"
adressLabel.text = "some adress"
phone = "+49123456 69 69804616"
coorPhone.setTitle("Duty Desk : \(phone)", forState: .Normal)
logoImage.image = UIImage(named: "flaggermany")


}
else if myBase == "LHR" {
titleLabel.text = "LONDON"
adressLabel.text = "some adress"
phone = "+44123456"
coorPhone.setTitle("Duty Desk : \(phone)", forState: .Normal)
logoImage.image = UIImage(named: "flaguk")

}
}
}

After that you can use it this way into another function:

@IBAction func phoneButtonPressed(sender: AnyObject) {

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

Hope this will help you.

Swift How do I get the dialer to open with phone number displayed?

Use this:

let phoneNumber = "+123123123"
let numberUrl = URL(string: "tel://\(phoneNumber)")!
if UIApplication.shared.canOpenURL(numberUrl) {
UIApplication.shared.open(numberUrl)
}

Related links:

  1. https://developer.apple.com/library/archive/releasenotes/General/RN-iOSSDK-10.3/
  2. https://developer.apple.com/documentation/uikit/uiapplication/1648685-open

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 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

Swift 3: How to give option to choose the phone number to call

As and add on for weissja19 answer, you should add a canopenurl check before calling the openurl call itself. Also neater if the user does not see an action he cannot perform. I'd recommend you to use a code like this.

@IBAction func makeCall(_ sender: Any) {
print ("------Phone Number-----")
print(landline)
print(phoneNumber)

let phone = "tel://";
let lline = landline
let url:NSURL = NSURL(string:phone+phoneNumber)!
let url2:NSURL = NSURL(string: phone+landline)!

let alert = UIAlertController(title: 'Choose a number to call', message: 'Please choose which number you want to call', preferredStyle: .alert)
if UIApplication.shared.canOpenUrl(url as URL) {
let firstNumberAction = UIAlertAction(title: "Number 1", style: .default, handler: { _ in
UIApplication.shared.openURL(url as URL)
})
alert.addAction(firstNumberAction)
}

if UIApplication.shared.canOpenUrl(url2 as URL) {
let secondNumberAction = UIAlertAction(title: "Number 2", style: .default, handler: { _ in
UIApplication.shared.openURL(url2 as URL)
})
alert.addAction(secondNumberAction)
}
if alert.actions.count == 0 {
alert.title = "No numbers to call"
alert.message = ""
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
} else {
alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
}
self.presentViewController(alert, animated: true, completion: nil)
}


Related Topics



Leave a reply



Submit