简体   繁体   English

字符串为tel:// *#06#的URL在Swift中返回nil

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

I am using the below code in Swift 4.x: 我在Swift 4.x中使用以下代码:

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

This gives url as nil. 这使url为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. 为防止用户恶意重定向电话或更改电话或帐户的行为,“电话”应用程序支持tel方案中的大多数(但不是全部)特殊字符。 Specifically, if a URL contains the * or # characters, the Phone app does not attempt to dial the corresponding phone number. 具体来说, 如果URL包含*或#字符,则“电话”应用程序不会尝试拨打相应的电话号码。

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. 请注意,对于其他字符,可以使用addingPercentEncoding方法对特殊字符进行转义,这将返回原始字符串的正确转义版本。

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)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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