我现在正在将电话号码保存为字符串,并且它使用 json 对象。
{
userFirstName: 'Jamie',
phone: '019-443 4444',
}
我将 json 对象保存到名为 phoneNumber
的变量中
var phoneNumber: String?
所以我尝试打印出电话号码,结果成功了
@IBAction func calledPassenger(_ sender: MyButton) {
if let phone = phoneNumber {
print(phone)
}
}
xcode控制台上的结果是019-443 4444
当我尝试使用 NSURL 时,我得到了意想不到的 nil
@IBAction func calledPassenger(_ sender: MyButton) {
if let phone = phoneNumber {
if let phoneCallURL:URL = URL(string:"tel://\(phone)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
if #available(iOS 10.0, *) {
application.open(phoneCallURL, options: [:], completionHandler: nil)
} else {
// Fallback on earlier versions
};
}
}
}
}
现在有什么问题吗?
最佳答案
无需使用“-”字符。 另外,如果您想要默认格式(不带空格),请使用 URL(string:"telprompt://"+phone.replacingOccurrences(of: "", with: ""))
关于ios - 尝试获取电话号码时不断获取 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46707730/