简体   繁体   中英

URL with string tel://*#06# is returning nil in Swift

I am using the below code in Swift 4.x:

let url = URL(string: "tel://*#06#")

This gives url as nil. Just this particular number has some problem. Can someone tell me how to solve this and why is this even a problem in the first place.

According to apple documentation here :

To prevent users from maliciously redirecting phone calls or changing the behavior of a phone or account, the Phone app supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone app does not attempt to dial the corresponding phone number.

Please note that for other characters, you can use addingPercentEncoding method to escape special characters which returns a properly escaped version of your original string.

Example:

let encoding = phoneNumberString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

正如@Ketan Ordera所建议的那样,即使在使用URLEncoding扩展了URL之后,也无法在iOS中运行USSD代码。

To make a call use the next function:

 static func callPhone (phone:String){

        guard let number = URL(string: "telprompt://" + phone) else { return }
        UIApplication.shared.open(number)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM