[英]How to strip phone number without ( ) or - in swift 3?
I am coming to a problem where I need to strip phoneNumber on my table view cell without ()
or -
so, I want to look this way 5555555555
, so when the user clicks on the phone number it should call that particular number. 我遇到了一个问题,我需要在没有()
情况下在我的表格视图单元格上删除phoneNumber或-
所以,我想这样看5555555555
,所以当用户点击电话号码时,它应该调用该特定号码。
Here is my code: 这是我的代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
if indexPath.row == 2 {
print("its working...")
guard let phoneNumber = self.place.phoneNumber,
phoneNumber.count > 0,
let url = URL(string: "tel:\(phoneNumber)")
else { return }
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
} else { return }
}
Your goal is to create a valid tel:
URL. 您的目标是创建有效的tel:
URL。 You do not need to strip the formatting of the phone number. 您无需删除电话号码的格式。 You just need to properly escape the special characters. 你只需要正确地逃避特殊字符。
Replace: 更换:
let url = URL(string: "tel:\(phoneNumber)")
with: 有:
let url = URL(string: "tel:\(phoneNumber.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!)")
你可以用字符串中的数字去掉
let number = phoneNumber.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.