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 !

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Husam
  • 8,149
  • 3
  • 38
  • 45
  • Phone numbers with hash or asterisk characters may not be allowed in urls: https://stackoverflow.com/questions/4660951/how-to-use-tel-with-star-asterisk-or-hash-pound-on-ios – paulvs Nov 01 '17 at 19:48
  • Yes, but In iOS 11 It's allowed. – Husam Nov 01 '17 at 19:50

2 Answers2

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"
Charles Srstka
  • 16,665
  • 3
  • 34
  • 60
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
                
            })
        }
    }
}
Community
  • 1
  • 1
Husam
  • 8,149
  • 3
  • 38
  • 45
  • 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`. – paulvs Nov 01 '17 at 19:50
  • Yes worked, the call prompt decoded the url automatically – Husam Nov 01 '17 at 19:51
  • 2
    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. – Charles Srstka Nov 01 '17 at 19:51
  • Why does the initial value of `u` contain `tel://`? Just encode actual number, not the scheme. And `tel` URLs do not use the `//`. – rmaddy Nov 01 '17 at 20:29