use*_*707 68 phone-call ios swift swift3 ios10
我希望我的应用程序能够在单击按钮时调用某个数字.我试图谷歌它但是到目前为止似乎没有iOS 10(openURL已经消失).有人能为我举个例子吗?例如:
@IBAction func callPoliceButton(_ sender: UIButton) {
// Call the local Police department
}
Run Code Online (Sandbox Code Playgroud)
Par*_*oja 140
你可以这样打电话:
if let url = URL(string: "tel://\(number)") {
UIApplication.shared.openURL(url)
}
Run Code Online (Sandbox Code Playgroud)
对于Swift 3+,您可以使用
guard let number = URL(string: "tel://" + number) else { return }
UIApplication.shared.open(number)
OR
UIApplication.shared.open(number, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)
请确保你已经擦洗你的手机串号删除的任何实例(
,)
,-
,或space
.
Vas*_*huk 58
通过电话号码验证拨打电话
Xcode 9.2,Swift 4
extension String {
enum RegularExpressions: String {
case phone = "^\\s*(?:\\+?(\\d{1,3}))?([-. (]*(\\d{3})[-. )]*)?((\\d{3})[-. ]*(\\d{2,4})(?:[-.x ]*(\\d+))?)\\s*$"
}
func isValid(regex: RegularExpressions) -> Bool { return isValid(regex: regex.rawValue) }
func isValid(regex: String) -> Bool { return range(of: regex, options: .regularExpression) != nil }
func onlyDigits() -> String {
let filtredUnicodeScalars = unicodeScalars.filter { CharacterSet.decimalDigits.contains($0) }
return String(String.UnicodeScalarView(filtredUnicodeScalars))
}
func makeAColl() {
guard isValid(regex: .phone),
let url = URL(string: "tel://\(self.onlyDigits())"),
UIApplication.shared.canOpenURL(url) else { return }
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
}
Run Code Online (Sandbox Code Playgroud)
"+1-(800)-123-4567".makeAColl()
Run Code Online (Sandbox Code Playgroud)
"phone:+1(617)111-22-33!".makeAColl() // Will extract "+1(617)111-22-33" and make a call
Run Code Online (Sandbox Code Playgroud)
Kir*_*hav 14
针对Swift 3进行了更新:
如果您想拨打电话,请使用以下简单的代码行:
//功能定义:
func makeAPhoneCall() {
let url: NSURL = URL(string: "TEL://1234567890")! as NSURL
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
Run Code Online (Sandbox Code Playgroud)
//函数调用:[在代码中的任何位置使用]
self.makeAPhoneCall()
Run Code Online (Sandbox Code Playgroud)
注意:请在真实设备上运行该应用,因为它无法在模拟器上运行.
Abd*_*ich 11
在Swift 4.2中
func dialNumber(number : String) {
if let url = URL(string: "tel://\(number)"),
UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler:nil)
} else {
UIApplication.shared.openURL(url)
}
} else {
// add error message here
}
}
Run Code Online (Sandbox Code Playgroud)
如下所示
dialNumber(number: "+921111111222")
Run Code Online (Sandbox Code Playgroud)
希望这有帮助.
if let phoneCallURL:URL = URL(string: "tel:\(strPhoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
let alertController = UIAlertController(title: "MyApp", message: "Are you sure you want to call \n\(self.strPhoneNumber)?", preferredStyle: .alert)
let yesPressed = UIAlertAction(title: "Yes", style: .default, handler: { (action) in
application.openURL(phoneCallURL)
})
let noPressed = UIAlertAction(title: "No", style: .default, handler: { (action) in
})
alertController.addAction(yesPressed)
alertController.addAction(noPressed)
present(alertController, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
错误地我的回答是错误的,请查看这个:你可以使用这个:
guard let url = URL(string: "tel://\(yourNumber)") else {
return //be safe
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
Run Code Online (Sandbox Code Playgroud)
我们需要检查我们是否在iOS 10或更高版本上因为'openURL'在iOS 10.0中已被弃用
归档时间: |
|
查看次数: |
72687 次 |
最近记录: |