可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has an answer here:
-
Calling a phone number in swift
18 answers
I want my app to be able to call a certain number when a button is clicked. I've tried to google it but there doesn't seem to have one for iOS 10 so far (where openURL is gone). Can someone put an example for me on how to do so? For instance like:
@IBAction func callPoliceButton(_ sender: UIButton) {
// Call the local Police department
}
回答1:
You can call like this:
if let url = URL(string: "tel://\(number)") {
UIApplication.shared.openURL(url)
}
For Swift 3+, you can use like
guard let number = URL(string: "tel://" + number) else { return }
UIApplication.shared.open(number)
OR
UIApplication.shared.open(number, options: [:], completionHandler: nil)
Make sure you've scrubbed your phone number string to remove any instances of (
, )
, -
, or space
.
回答2:
Task
Make a call with phone number validation
Details
Xcode 9.2, Swift 4
Solution
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 {
let matches = range(of: regex, options: .regularExpression)
return matches != nil
}
func onlyDigits() -> String {
let filtredUnicodeScalars = unicodeScalars.filter{CharacterSet.decimalDigits.contains($0)}
return String(String.UnicodeScalarView(filtredUnicodeScalars))
}
func makeAColl() {
if isValid(regex: .phone) {
if let url = URL(string: "tel://\(self.onlyDigits())"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
}
}
}
Usage
"+1-(800)-123-4567".makeAColl()
Sample for test
func test() {
isPhone("blabla")
isPhone("+1(222)333-44-55")
isPhone("+42 555.123.4567")
isPhone("+1-(800)-123-4567")
isPhone("+7 555 1234567")
isPhone("+7(926)1234567")
isPhone("(926) 1234567")
isPhone("+79261234567")
isPhone("926 1234567")
isPhone("9261234567")
isPhone("1234567")
isPhone("123-4567")
isPhone("123-89-01")
isPhone("495 1234567")
isPhone("469 123 45 67")
isPhone("8 (926) 1234567")
isPhone("89261234567")
isPhone("926.123.4567")
isPhone("415-555-1234")
isPhone("650-555-2345")
isPhone("(416)555-3456")
isPhone("202 555 4567")
isPhone("4035555678")
isPhone(" 1 416 555 9292")
}
private func isPhone(_ string: String) {
let result = string.isValid(regex: .phone)
print("\(result ? "✅" : "❌") \(string) | \(string.onlyDigits()) | \(result ? "[a phone number]" : "[not a phone number]")")
}
Result
回答3:
Updated for Swift 3:
used below simple lines of code, if you want to make a phone call:
// function defination:
func makeAPhoneCall() {
let url: NSURL = URL(string: "TEL://1234567890")! as NSURL
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
// function call: [Used anywhere in your code]
self.makeAPhoneCall()
Note: Please run the app on a real device because it won't work on the simulator.
回答4:
By mistake my answer was misplaced, please checkout this one:
You can use this:
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)
}
We need to check whether we're on iOS 10 or later As 'openURL' was deprecated in iOS 10.0
回答5:
In 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
}
}
Call this like below
dialNumber(number: "+921111111222")
Hope this help.
回答6:
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)
}
}