0

I wrote some code to initiate phone call from String like the attached image. [function: phoneCallInitiation][1]

Some phone number can be initiated well with the format such as 02-123-1234, but some phone number with 031-123-1234 cannot be initiated but I can see phoneNumberString is working well.

Can u guess any reason to work like this?

func callButtonPressed () {
    let alertViewController = UIAlertController(title: NSLocalizedString("Call", comment: ""), message: "\(storeToDisplay.phoneNumber!)", preferredStyle: .alert)
    alertViewController.addAction(UIAlertAction(title: "Call", style: .default, handler: { (alertAction) -> Void in
        let phoneNumberString = "tel://\(self.storeToDisplay.phoneNumber!)"
        print("This is phonenumber: \(phoneNumberString)")
        if let url = URL(string: phoneNumberString) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else {
            print("WHAT Happenes")
        }
    }))
    alertViewController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (alertAction) -> Void in
        alertViewController.dismiss(animated: true, completion: nil)
    }))
    present(alertViewController, animated: true, completion: nil)
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

0

UIApplication only works or accepts if your number is ("1234567890"). So you need to remove " " and "-" from the string.

Hope below link will be helpful for you.

https://stackoverflow.com/a/39849592/1811281

https://stackoverflow.com/a/27515978/1811281

Community
  • 1
  • 1
Lalit
  • 264
  • 3
  • 10
0

If you are using your application in simulator then you will not find anything happening. Try to run on actual device if you are using simulator.

try this

 let replaced = self.storeToDisplay.phoneNumber!.replacingOccurrences(of: "-", with: "")   
  let phoneNumberString = "tel://\(replaced)"

and add

if let url = URL(string: "telprompt://\(replaced)") {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }

else if you want to call direct

 if let url = URL(string: "tel://\(replaced)") {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • Thanks for your help - sometimes it works when string includes '-' but better remove all. In my case, string contains blank in the last part so I did not recognize it. Thanks for your help! – Junghoon Choi Feb 20 '17 at 06:12