【问题标题】:How to make phone call in iOS 10 using Swift? [duplicate]如何使用 Swift 在 iOS 10 中拨打电话? [复制]
【发布时间】:2017-02-25 23:08:15
【问题描述】:

我希望我的应用能够在单击按钮时呼叫某个号码。我试过用谷歌搜索它,但到目前为止似乎还没有适用于 iOS 10 的版本(openURL 已经消失了)。有人可以为我举个例子吗?比如:

@IBAction func callPoliceButton(_ sender: UIButton) {
    // Call the local Police department
}

【问题讨论】:

  • 谢谢!但是,我尝试了相同的确切代码,当我将 google.com 更改为“tel://120390129391824812988238197”时,单击按钮时没有任何反应。有什么我想念的吗?
  • 我期待它有一些弹出窗口并说“致电120390129391824812988238197?”

标签: ios swift swift3 ios10 phone-call


【解决方案1】:

你可以这样调用:

 if let url = URL(string: "tel://\(number)") {
     UIApplication.shared.openURL(url)
 }

对于Swift 3+,你可以使用like

guard let number = URL(string: "tel://" + number) else { return }
UIApplication.shared.open(number)

UIApplication.shared.open(number, options: [:], completionHandler: nil)

确保您已清除电话号码字符串以删除所有 ()-space 的实例。

【讨论】:

  • openURL 方法在 iOS 10 中已弃用。OP 正在询问替换是什么。
  • optionscompletionHandler 具有默认值,因此您实际上可以执行 UIApplication.shared.open(number)
  • @Paolo 没错。
  • @ParthAdroja 上面的数字是多少
  • @DilipTiwari Number 将是字符串。
【解决方案2】:

任务

使用电话号码验证拨打电话

详情

测试日期:

  • Swift 5.2、Xcode 11.4 (11E146)

解决方案

// MARK: DataDetector

class DataDetector {

    private class func _find(all type: NSTextCheckingResult.CheckingType,
                             in string: String, iterationClosure: (String) -> Bool) {
        guard let detector = try? NSDataDetector(types: type.rawValue) else { return }
        let range = NSRange(string.startIndex ..< string.endIndex, in: string)
        let matches = detector.matches(in: string, options: [], range: range)
        loop: for match in matches {
            for i in 0 ..< match.numberOfRanges {
                let nsrange = match.range(at: i)
                let startIndex = string.index(string.startIndex, offsetBy: nsrange.lowerBound)
                let endIndex = string.index(string.startIndex, offsetBy: nsrange.upperBound)
                let range = startIndex..<endIndex
                guard iterationClosure(String(string[range])) else { break loop }
            }
        }
    }

    class func find(all type: NSTextCheckingResult.CheckingType, in string: String) -> [String] {
        var results = [String]()
        _find(all: type, in: string) {
            results.append($0)
            return true
        }
        return results
    }

    class func first(type: NSTextCheckingResult.CheckingType, in string: String) -> String? {
        var result: String?
        _find(all: type, in: string) {
            result = $0
            return false
        }
        return result
    }
}

// MARK: PhoneNumber

struct PhoneNumber {
    private(set) var number: String
    init?(extractFrom string: String) {
        guard let phoneNumber = PhoneNumber.first(in: string) else { return nil }
        self = phoneNumber
    }

    private init (string: String) { self.number = string }

    func makeACall() {
        guard let url = URL(string: "tel://\(number.onlyDigits())"),
            UIApplication.shared.canOpenURL(url) else { return }
        if #available(iOS 10, *) {
            UIApplication.shared.open(url)
        } else {
            UIApplication.shared.openURL(url)
        }
    }

    static func extractAll(from string: String) -> [PhoneNumber] {
        DataDetector.find(all: .phoneNumber, in: string)
            .compactMap {  PhoneNumber(string: $0) }
    }

    static func first(in string: String) -> PhoneNumber? {
        guard let phoneNumberString = DataDetector.first(type: .phoneNumber, in: string) else { return nil }
        return PhoneNumber(string: phoneNumberString)
    }
}

extension PhoneNumber: CustomStringConvertible { var description: String { number } }

// MARK: String extension

extension String {

    // MARK: Get remove all characters exept numbers

    func onlyDigits() -> String {
        let filtredUnicodeScalars = unicodeScalars.filter { CharacterSet.decimalDigits.contains($0) }
        return String(String.UnicodeScalarView(filtredUnicodeScalars))
    }

    var detectedPhoneNumbers: [PhoneNumber] { PhoneNumber.extractAll(from: self) }
    var detectedFirstPhoneNumber: PhoneNumber? { PhoneNumber.first(in: self) }
}

用法

PhoneNumber(extractFrom: "+1-(800)-123-4567")?.makeACall()

PhoneNumber.extractAll(from: "+1-(800)-123-4567 bla bla 1(617)111-22-33").last?.makeACall()

PhoneNumber.first(in: "+1-(800)-123-4567 bla bla 1(617)111-22-33")?.makeACall()

"+1-(800)-123-4567 bla bla 1(617)111-22-33".detectedPhoneNumbers[1].makeACall()
"+1-(800)-123-4567 bla bla 1(617)111-22-33".detectedFirstPhoneNumber?.makeACall()

完整样本

不要忘记在此处粘贴解决方案代码

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")
    isPhone("1(617)111-22-33!")
    isPhone("+44 1838 300284")
    isPhone("+44 1838 300284, 1 416 555 9292")
    isPhone("+44 1838 3d0384, 1 416 555 9292!")
}

private func isPhone(_ string: String) {
    let phoneNumbers = PhoneNumber.extractAll(from: string)
    let result = !phoneNumbers.isEmpty
    print("\(result ? "✅" : "❌") \(string) | detected phones: \(phoneNumbers)")
}

结果

❌ blabla | detected phones: []
✅ +1(222)333-44-55 | detected phones: [+1(222)333-44-55]
✅ +42 555.123.4567 | detected phones: [555.123.4567]
✅ +1-(800)-123-4567 | detected phones: [+1-(800)-123-4567]
✅ +7 555 1234567 | detected phones: [+7 555 1234567]
✅ +7(926)1234567 | detected phones: [+7(926)1234567]
✅ (926) 1234567 | detected phones: [(926) 1234567]
✅ +79261234567 | detected phones: [+79261234567]
✅ 926 1234567 | detected phones: [926 1234567]
✅ 9261234567 | detected phones: [9261234567]
✅ 1234567 | detected phones: [1234567]
✅ 123-4567 | detected phones: [123-4567]
✅ 123-89-01 | detected phones: [123-89-01]
✅ 495 1234567 | detected phones: [495 1234567]
✅ 469 123 45 67 | detected phones: [469 123 45 67]
✅ 8 (926) 1234567 | detected phones: [8 (926) 1234567]
✅ 89261234567 | detected phones: [89261234567]
✅ 926.123.4567 | detected phones: [926.123.4567]
✅ 415-555-1234 | detected phones: [415-555-1234]
✅ 650-555-2345 | detected phones: [650-555-2345]
✅ (416)555-3456 | detected phones: [(416)555-3456]
✅ 202 555 4567 | detected phones: [202 555 4567]
✅ 4035555678 | detected phones: [4035555678]
✅  1 416 555 9292 | detected phones: [1 416 555 9292]
✅ 1(617)111-22-33! | detected phones: [1(617)111-22-33]
✅ +44 1838 300284 | detected phones: [+44 1838 300284]
✅ +44 1838 300284, 1 416 555 9292 | detected phones: [+44 1838 300284, 1 416 555 9292]
✅ +44 1838 3d0384, 1 416 555 9292! | detected phones: [1 416 555 9292]

【讨论】:

  • 我用花哨的答案和鲜艳的颜色/表情符号直接投票...然后... ❌ +44 1838 300284 | 441838300284 | [不是电话号码]。这对我不起作用,并且对所有内容都返回 false
  • @paul_f 您是否尝试在示例上运行代码?是不是也行不通?
  • 测试工作正常,不管我的测试用例是什么,它根本不起作用:例如“+44 1838 300284”。我认为数字的间距是问题。这是一个常见的间距。
  • @paul_f 检查答案。我更新了代码
  • @SivabalaaJothibose 再试一次。我更新了代码
【解决方案3】:

在 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 
   }
}

像下面这样调用它

dialNumber(number: "+921111111222")

希望对您有所帮助。

【讨论】:

  • 这个已经发过很多次了……
【解决方案4】:

为 Swift 3 更新:

在简单的代码行下面使用,如果你想打电话:

//函数定义:

func makeAPhoneCall()  {
    let url: NSURL = URL(string: "TEL://1234567890")! as NSURL
    UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}

// 函数调用:[在代码中的任何地方使用]

self.makeAPhoneCall()

注意:请在真机上运行该应用,因为它无法在模拟器上运行。

【讨论】:

  • 它不考虑第一个位置的0
【解决方案5】:

错误地我的答案放错了地方,请检查一下: 你可以使用这个:

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)
}

我们需要检查我们是否在 iOS 10 或更高版本上因为 'openURL' 在 iOS 10.0 中已被弃用

【讨论】:

    【解决方案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)
            }
        }
    

    【讨论】:

    • 更好的实现。
    猜你喜欢
    • 1970-01-01
    • 2014-08-06
    • 2010-10-14
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多