快速拨打电话号码

Tho*_*nez 74 phone-call phone-number ios swift

我试图拨打一个不使用特定号码的号码,而是拨打变量中的号码,或者至少告诉它拨打手机中的号码.在变量中调用的这个数字是我使用解析器或从网站sql中获取的数字.我做了一个按钮试图通过功能调用存储在变量中的电话号码,但无济于事.什么都有帮助谢谢!

    func callSellerPressed (sender: UIButton!){
 //(This is calls a specific number)UIApplication.sharedApplication().openURL(NSURL(string: "tel://######")!)

 // This is the code I'm using but its not working      
 UIApplication.sharedApplication().openURL(NSURL(scheme: NSString(), host: "tel://", path: busPhone)!)

        }
Run Code Online (Sandbox Code Playgroud)

Tho*_*ler 166

试一试:

if let url = NSURL(string: "tel://\(busPhone)") where UIApplication.sharedApplication().canOpenURL(url) {
  UIApplication.sharedApplication().openURL(url)
}
Run Code Online (Sandbox Code Playgroud)

假设电话号码在busPhone.

NSURLinit(string:)返回一个可选的,所以通过使用if let我们确保url是一个NSURL(而不是NSURL?通过返回的init).


对于Swift 3:

if let url = URL(string: "tel://\(busPhone)"), UIApplication.shared.canOpenURL(url) {
    if #available(iOS 10, *) {
        UIApplication.shared.open(url)
    } else {
        UIApplication.shared.openURL(url)
    }
}
Run Code Online (Sandbox Code Playgroud)

我们需要检查我们是否在iOS 10或更高版本上,因为:

'openURL'在iOS 10.0中已弃用

  • 好吧,我想我知道问题是什么,但不知道如何解决它.从URL中提取的数字完全按照这个123 456-7890编写,而不是像1234567890那样.如何翻译tel://以读取它将识别的形式的URL? (2认同)

Zor*_*ayr 63

iOS 10中的自包含解决方案,Swift 3:

private func callNumber(phoneNumber:String) {

  if let phoneCallURL = URL(string: "tel://\(phoneNumber)") {

    let application:UIApplication = UIApplication.shared
    if (application.canOpenURL(phoneCallURL)) {
        application.open(phoneCallURL, options: [:], completionHandler: nil)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

你应该可以用来callNumber("7178881234")打电话.


Tej*_*eja 15

斯威夫特4,

private func callNumber(phoneNumber:String) {

    if let phoneCallURL = URL(string: "telprompt://\(phoneNumber)") {

        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
                 application.openURL(phoneCallURL as URL)

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


gra*_*spo 13

斯威夫特 5:iOS >= 10.0

这个解决方案是零保存。

仅适用于物理设备。

private func callNumber(phoneNumber: String) {
    guard let url = URL(string: "telprompt://\(phoneNumber)"),
        UIApplication.shared.canOpenURL(url) else {
        return
    }
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Run Code Online (Sandbox Code Playgroud)

  • 下面有人说,如果您使用“telprompt”而不是“tel”,您的应用程序将被拒绝。 (5认同)

小智 11

Swift 3.0和ios 10或更早版本

func phone(phoneNum: String) {
    if let url = URL(string: "tel://\(phoneNum)") {
        if #available(iOS 10, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url as URL)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Tho*_*nez 9

好的,我得到了帮助并想出来了.此外,我还放了一个漂亮的小警报系统,以防电话号码无效.我的问题是我说的是正确的,但数字有空格和不需要的字符,如("123 456-7890").如果您的号码是("1234567890"),则UIApplication仅适用或接受.因此,您基本上通过创建一个新变量来仅删除数字来删除空格和无效字符.然后使用UIApplication调用这些数字.

func callSellerPressed (sender: UIButton!){
        var newPhone = ""

        for (var i = 0; i < countElements(busPhone); i++){

            var current:Int = i
            switch (busPhone[i]){
                case "0","1","2","3","4","5","6","7","8","9" : newPhone = newPhone + String(busPhone[i])
                default : println("Removed invalid character.")
            }
        }

        if  (busPhone.utf16Count > 1){

        UIApplication.sharedApplication().openURL(NSURL(string: "tel://" + newPhone)!)
        }
        else{
            let alert = UIAlertView()
            alert.title = "Sorry!"
            alert.message = "Phone number is not available for this business"
            alert.addButtonWithTitle("Ok")
                alert.show()
        }
        }
Run Code Online (Sandbox Code Playgroud)

  • 我认为你也应该允许'+'符号. (6认同)

Dip*_*jar 9

以上答案部分正确,但"tel://"只有一个问题.通话结束后,它将返回主屏幕,而不是我们的应用程序.所以最好使用"telprompt://",它将返回到应用程序.

var url:NSURL = NSURL(string: "telprompt://1234567891")!
UIApplication.sharedApplication().openURL(url)
Run Code Online (Sandbox Code Playgroud)


小智 7

我在我的应用程序中使用此方法,它工作正常.我希望这对你也有帮助.

func makeCall(phone: String) {
    let formatedNumber = phone.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
    let phoneUrl = "tel://\(formatedNumber)"
    let url:NSURL = NSURL(string: phoneUrl)!
    UIApplication.sharedApplication().openURL(url)
}
Run Code Online (Sandbox Code Playgroud)


LuA*_*dre 7

Swift 3,iOS 10

func call(phoneNumber:String) {
        let cleanPhoneNumber = phoneNumber.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
        let urlString:String = "tel://\(cleanPhoneNumber)"
        if let phoneCallURL = URL(string: urlString) {
            if (UIApplication.shared.canOpenURL(phoneCallURL)) {
                UIApplication.shared.open(phoneCallURL, options: [:], completionHandler: nil)
            }
        }
  }
Run Code Online (Sandbox Code Playgroud)


Mah*_*iya 7

许多其他答案不适用于 Swift 5。以下是 Swift 5 的代码更新:

let formattedNumber = phoneNumberVariable.components(separatedBy: NSCharacterSet.decimalDigits.inverted).joined(separator: "")

if let url = NSURL(string: ("tel:" + (formattedNumber)!)) {
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url as URL)
    }
}
Run Code Online (Sandbox Code Playgroud)

附:

  1. 对于大多数答案,我无法在设备上收到提示。上面的代码成功的能够显示提示。
  2. 像大多数答案一样,tel:之后没有// 。而且效果很好。


Ven*_*enk 5

在斯威夫特 3 中,

if let url = URL(string:"tel://\(phoneNumber)"), UIApplication.shared.canOpenURL(url) {
     UIApplication.shared.openURL(url)
}
Run Code Online (Sandbox Code Playgroud)


Emm*_*ett 5

我正在使用带有数字验证的 swift 3 解决方案

var validPhoneNumber = ""
    phoneNumber.characters.forEach {(character) in
        switch character {
        case "0"..."9":
            validPhoneNumber.characters.append(character)
        default:
            break
        }
    }

    if UIApplication.shared.canOpenURL(URL(string: "tel://\(validNumber)")!){
        UIApplication.shared.openURL(URL(string: "tel://\(validNumber)")!)
    }
Run Code Online (Sandbox Code Playgroud)