0

Issue explained through code.

    let u = "tel://*111*11111111111#" //Works perfectly on iOS11 and later
    let u = "tel://*111#11111111111#" //Doesn't work, Can't create URL

    let app = UIApplication.shared
    if let url = URL(string:u) {
        if app.canOpenURL(url) {
            app.open(url, options: [:], completionHandler: { (finished) in 
            })
        }
    }

** If I encode the string then canOpenURL() fails !

2
  • Phone numbers with hash or asterisk characters may not be allowed in urls: stackoverflow.com/questions/4660951/… Commented Nov 1, 2017 at 19:48
  • Yes, but In iOS 11 It's allowed. Commented Nov 1, 2017 at 19:50

2 Answers 2

1

Just use URLComponents. Handles percent escapes automatically for you and everything.

var comps = URLComponents()

comps.scheme = "tel"
comps.host = "*111#11111111111#"

print(comps.url!) // prints "tel://*111%2311111111111%23"
Sign up to request clarification or add additional context in comments.

Comments

0

Problem fixed,

Number should be encoded separately without tel://

let u = "tel://*111#11111111111#"
if let encoded = u.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) {
    let u = "tel://\(encoded)"
    if let url = URL(string:u) {
        if app.canOpenURL(url) {
            app.open(url, options: [:], completionHandler: { (finished) in
                
            })
        }
    }
}

4 Comments

Does the call work even though you're percent-escaping the hash? The final URL will be tel://tel%3A%2F%2F*111%2311111111111%23.
Yes worked, the call prompt decoded the url automatically
IMO, URLComponents is a simpler and cleaner way to do this. URLComponents is also more up to date, since it conforms to RFC 3986, unlike URL(string:) which the documentation states to conform to older RFCs.
Why does the initial value of u contain tel://? Just encode actual number, not the scheme. And tel URLs do not use the //.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.