我有一个保存电话号码的数据库(固定电话或手机或两者)。根据记录提取,如果用户想要拨打号码并且记录同时包含号码,移动电话和固定电话,如何给用户选择号码。
@IBAction func makeCall(_ sender: Any) {
print ("------Phone Number-----")
print(landline)
print(phoneNumber)
let phone = "tel://";
let lline = landline
let url:NSURL = NSURL(string:phone+phoneNumber)!
let url2:NSURL = NSURL(string: phone+landline)!
UIApplication.shared.openURL(url as URL)
UIApplication.shared.openURL(url2 as URL)
}
使用上面的代码,它会拨打这两个号码,但是想让用户选择其中一个号码。
答案 0 :(得分:3)
您可以使用UIAlertController为用户提供选择:
@IBAction func makeCall(_ sender: Any) {
print ("------Phone Number-----")
print(landline)
print(phoneNumber)
let phone = "tel://";
let lline = landline
let url:NSURL = NSURL(string:phone+phoneNumber)!
let url2:NSURL = NSURL(string: phone+landline)!
let alert = UIAlertController(title: "Choose a number to call", message: "Please choose which number you want to call", preferredStyle: .alert)
let firstNumberAction = UIAlertAction(title: "Number 1", style: .default) { (action) in
UIApplication.shared.openURL(url as URL)
}
let secondNumberAction = UIAlertAction(title: "Number 2", style: .default) { (action) in
UIApplication.shared.openURL(url2 as URL)
}
alert.addAction(firstNumberAction)
alert.addAction(secondNumberAction)
self.present(alert, animated: true)
}
如果您愿意,您当然也可以添加取消操作。
答案 1 :(得分:3)
作为weissja19的答案,你应该在调用openurl调用之前添加一个canopenurl检查。如果用户没有看到他无法执行的操作,也更整洁。我建议你使用这样的代码。
@IBAction func makeCall(_ sender: Any) {
print ("------Phone Number-----")
print(landline)
print(phoneNumber)
let phone = "tel://";
let lline = landline
let url:NSURL = NSURL(string:phone+phoneNumber)!
let url2:NSURL = NSURL(string: phone+landline)!
let alert = UIAlertController(title: 'Choose a number to call', message: 'Please choose which number you want to call', preferredStyle: .alert)
if UIApplication.shared.canOpenUrl(url as URL) {
let firstNumberAction = UIAlertAction(title: "Number 1", style: .default, handler: { _in
UIApplication.shared.openURL(url as URL)
})
alert.addAction(firstNumberAction)
}
if UIApplication.shared.canOpenUrl(url2 as URL) {
let secondNumberAction = UIAlertAction(title: "Number 2", style: .default, handler: { _in
UIApplication.shared.openURL(url2 as URL)
})
alert.addAction(secondNumberAction)
}
if alert.actions.count == 0 {
alert.title = "No numbers to call"
alert.message = ""
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
} else {
alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
}
self.presentViewController(alert, animated: true, completion: nil)
}
答案 2 :(得分:0)
这两个代码都有效。以下是工作代码
set sftp:connect-program "ssh -ax -i key-file"
非常感谢weissja19和Ben Ong