快速拨打电话号码

时间:2014-12-02 21:59:12

标签: ios swift phone-number phone-call

我试图拨打不使用特定号码的号码,而是拨打变量中的号码,或至少告诉它拨打手机中的号码。在变量中调用的这个数字是我使用解析器或从网站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)!)

        }

23 个答案:

答案 0 :(得分:160)

试试吧:

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

假设电话号码在busPhone

NSURL' init(string:)返回一个可选项,因此我们使用if let确保urlNSURL(而不是NSURL? init返回的if let url = URL(string: "tel://\(busPhone)"), UIApplication.shared.canOpenURL(url) { if #available(iOS 10, *) { UIApplication.shared.open(url) } else { UIApplication.shared.openURL(url) } }


对于Swift 3:

{{1}}

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

  

'的OpenURL'在iOS 10.0中已弃用

答案 1 :(得分:59)

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

您应该可以使用callNumber("7178881234")拨打电话。

答案 2 :(得分:11)

Swift 3.0和ios 10或更早版

req.end()

答案 3 :(得分:10)

雨燕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)

            }
        }
    }
}

答案 4 :(得分:9)

< p>以上答案部分正确,但使用" tel://"只有一个问题。通话结束后,它将返回主屏幕,而不是我们的应用程序。最好使用" telprompt://",它将返回到应用。< / p> < pre>< code> var url:NSURL = NSURL(字符串:" telprompt:// 1234567891")! UIApplication.sharedApplication()的OpenURL(URL) < /代码>< /预>

答案 5 :(得分:8)

好的,我得到了帮助并想出来了。此外,我还放了一个漂亮的小警报系统,以防电话号码无效。我的问题是我说的是正确的,但数字有空格和不需要的字符,如(“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()
        }
        }

答案 6 :(得分:6)

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

答案 7 :(得分:6)

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

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

答案 8 :(得分:6)

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

答案 9 :(得分:4)

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

{{1}}

答案 10 :(得分:4)

在Swift 3中,

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

答案 11 :(得分:3)

这是使用Swift 2.0更新@ Tom的答案 注意 - 这是我正在使用的整个CallComposer类。

class CallComposer: NSObject {

var editedPhoneNumber = ""

func call(phoneNumber: String) -> Bool {

    if phoneNumber != "" {

        for i in number.characters {

            switch (i){
                case "0","1","2","3","4","5","6","7","8","9" : editedPhoneNumber = editedPhoneNumber + String(i)
                default : print("Removed invalid character.")
            }
        }

    let phone = "tel://" + editedPhoneNumber
        let url = NSURL(string: phone)
        if let url = url {
            UIApplication.sharedApplication().openURL(url)
        } else {
            print("There was an error")
        }
    } else {
        return false
    }

    return true
 }
}

答案 12 :(得分:2)

在iOS 10中已弃用

openURL()。以下是新语法:

if let url = URL(string: "tel://\(busPhone)") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

答案 13 :(得分:2)

许多其他答案不适用于Swift5。以下是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)
    }
}

PS:

  1. 有了大多数答案,我无法在设备上得到提示。上面的代码成功显示了提示。
  2. 电话后没有 // ,就像大多数答案一样。而且效果很好。

答案 14 :(得分:1)

适用于swift 3.0

if let url = URL(string: "tel://\(number)"), UIApplication.shared.canOpenURL(url) {
    if #available(iOS 10, *) {
        UIApplication.shared.open(url)
    } else {
        UIApplication.shared.openURL(url)
    }
}
else {
    print("Your device doesn't support this feature.")
}

答案 15 :(得分:1)

Swift 3.0解决方案:

let formatedNumber = phone.components(separatedBy: NSCharacterSet.decimalDigits.inverted).joined(separator: "")
print("calling \(formatedNumber)")
let phoneUrl = "tel://\(formatedNumber)"
let url:URL = URL(string: phoneUrl)!
UIApplication.shared.openURL(url)

答案 16 :(得分:0)

以下是使用Scanner ...

将电话号码减少为有效组件的另一种方法
let number = "+123 456-7890"

let scanner = Scanner(string: number)

let validCharacters = CharacterSet.decimalDigits
let startCharacters = validCharacters.union(CharacterSet(charactersIn: "+#"))

var digits: NSString?
var validNumber = ""
while !scanner.isAtEnd {
    if scanner.scanLocation == 0 {
        scanner.scanCharacters(from: startCharacters, into: &digits)
    } else {
        scanner.scanCharacters(from: validCharacters, into: &digits)
    }

    scanner.scanUpToCharacters(from: validCharacters, into: nil)
    if let digits = digits as? String {
        validNumber.append(digits)
    }
}

print(validNumber)

// +1234567890

答案 17 :(得分:0)

Swift 3.0&amp; iOS 10 +

UIApplication.shared.openURL(url) 被改为 UIApplication.shared.open(_ url: URL, options:[:], completionHandler completion: nil)

选项完成处理程序是可选的,呈现:

UIApplication.shared.open(url)

https://developer.apple.com/reference/uikit/uiapplication/1648685-open

答案 18 :(得分:0)

对于Swift 3.1&amp;向后兼容的方法,这样做:

nil

答案 19 :(得分:0)

如果您的电话号码包含空格,请先将其删除!然后,您可以使用accepted answer's解决方案。

Flow

答案 20 :(得分:0)

  

对于Swift 4.2及更高版本

if let phoneCallURL = URL(string: "tel://\(01234567)"), UIApplication.shared.canOpenURL(phoneCallURL)
{
    UIApplication.shared.open(phoneCallURL, options: [:], completionHandler: nil)
}

答案 21 :(得分:0)

let formatedNumber = phone.components(separatedBy: NSCharacterSet.decimalDigits.inverted).joined(separator: "")
print("calling \(formatedNumber)")
let phoneUrl = "tel://\(formatedNumber)"
let url:URL = URL(string: phoneUrl)!
UIApplication.shared.openURL(url)

答案 22 :(得分:0)

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)
        }
    }
}
相关问题