0

I am trying to place a phone call with this code...

  let phone = detail.value(forKey: "Phone") as? String
    guard let number = URL(string: "telprompt://"  (phone)) else { return }
    UIApplication.shared.open(number, options: [:], completionHandler: nil)

I attempted to vary my code basically off this answer swift how to make phone call iOS 10?, but I am having difficulty creating a working/error-free function.

Originally my code went like

 guard let number = URL(string: "telprompt://"\(phone))...

however, Xcode directed a space between the end quote and open paranthensis for the phone variable while simultaneously deleting "\". Unfortunately, now I am left with the error in the title. A tweak in my code would be appreciated :D

Update 1: I have updated my code to

@IBAction func call(_ sender: Any)
{
   let phone = detail.value(forKey: "Phone") as? NSURL

    func makeCallToNumber(number: String){

        if let url = URL(string: "TEL://\(phone)"){
            UIApplication.shared.open(url , options: [:], completionHandler: nil)
        }
        else{
            print("Error")
        }
    }
}

yet the code is still not bringing up the dialer.

update 2:

I have switched my code to

let phone = detail.value(forKey: "Phone") as? String
    if let url = URL(string: "telprompt:\(String(describing: phone))") {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }

and while I have no errors, no call is being made and this appears in my console. message in console. Unsure what it means.

Community
  • 1
  • 1
Jon
  • 85
  • 1
  • 9

2 Answers2

0

Working example:

func callNumber(phoneNumber: String) {
    if let phoneCallURL = NSURL(string: "tel://\(phoneNumber.phoneToString())") {
        if UIApplication.shared.canOpenURL(phoneCallURL as URL) {
            UIApplication.shared.openURL(phoneCallURL as URL)
        }
    }
}

//removes "(", ")", "-", " " etc. and adds "+" for region code format 
extension String {
    func phoneToString() -> String {
        var value = "+"
        for character in self.characters {
            if Int(String(character)) != nil {
                value = value + String(character)
            }
        }
        return value
    }
}

phoneToString would format "+000 (000) 000" into "+000000000"

JuicyFruit
  • 2,638
  • 2
  • 18
  • 35
  • I've added these two lines to the top portion of your answer and I've still had poor luck achieving successful results. let phone = detail.value(forKey: "Phone") as? NSURL func callNumber(phoneNumber: String) { if let phoneCallURL = NSURL(string: "tel://\(String(describing: phone))") { – Jon Mar 14 '17 at 23:32
0

Try this easy code:

func makeCallToNumber(number: String){

        if let url = URL(string: "TEL://\(number)"){
            UIApplication.shared.open(url , options: [:], completionHandler: nil)
        }
        else{
            print("Error")
        }
}

Using guard and telprompt

func makeCallToNumber2(number: String){

        guard let url = URL(string: "telprompt://\(number)") else {
            print("Error")
            return
        }
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

Regards

agscastaneda
  • 174
  • 6
  • I'll play with the two pieces however, what is the difference between the easy code vs the code with guard and telpromt? – Jon Mar 14 '17 at 23:24
  • I've also updated my code to the easy portion of your easy code (in question) but I am still not having luck with any dialing. – Jon Mar 14 '17 at 23:29
  • 1. It should be `tel`, not `TEL`. 2. Don't use `//` in a `tel` or `telprompt` URL. – rmaddy Mar 14 '17 at 23:33
  • Both are Easy, sorry for my bad grammar, the functionality is the same, the difference is that guard allow to check the value and use out of If. @rmaddy both (TEL and tel work fine) and yes I'm agree with you regarding the use of //. Is preferable use URLComponents to build the url. – agscastaneda Mar 15 '17 at 03:48
  • Try to hard code a number using the code above and send a number like: `makeCallToNumber2 ("1234566")` or `makeCallToNumber ("+52 1234")` just for check the functionality. – agscastaneda Mar 15 '17 at 04:05