ios 快速拨打电话号码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27259824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:52:47  来源:igfitidea点击:

Calling a phone number in swift

iosswiftphone-numberphone-call

提问by Thomas Martinez

I'm trying to call a number not using specific numbers but a number that is being called in a variable or at least tell it to pull up the number in your phone. This number that is being called in a variable is a number that I retrieved by using a parser or grabbing from a website sql. I made a button trying to call the phone number stored in the variable with a function but to no avail. Anything will help thanks!

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

        }

回答by Thomas Müller

Just try:

你试一试:

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

assuming that the phone number is in busPhone.

假设电话号码在busPhone.

NSURL's init(string:)returns an Optional, so by using if letwe make sure that urlis a NSURL(and not a NSURL?as returned by the init).

NSURL'sinit(string:)返回一个 Optional,因此通过使用if let我们可以确保它url是 a NSURL(而不是由NSURL?返回的 a init)。



For Swift 3:

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

We need to check whether we're on iOS 10 or later because:

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

'openURL' was deprecated in iOS 10.0

'openURL' 在 iOS 10.0 中被弃用

回答by Zorayr

A self contained solution in iOS 10, Swift 3:

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

You should be able to use callNumber("7178881234")to make a call.

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

回答by Teja

Swift 4,

斯威夫特 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)

            }
        }
    }
}

回答by Gandom

Swift 3.0 and ios 10 or older

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

回答by Dipen Gajjar

The above answers are partially correct, but with "tel://" there is only one issue. After the call has ended, it will return to the homescreen, not to our app. So better to use "telprompt://", it will return to the app.

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

var url:NSURL = NSURL(string: "telprompt://1234567891")!
UIApplication.sharedApplication().openURL(url)

回答by Thomas Martinez

Okay I got help and figured it out. Also I put in a nice little alert system just in case the phone number is not valid. My issue was I was calling it right but the number had spaces and unwanted characters such as ("123 456-7890"). UIApplication only works or accepts if your number is ("1234567890"). So you basically remove the space and invalid characters by making a new variable to pull only the numbers. Then calls those numbers with the UIApplication.

好的,我得到了帮助并弄清楚了。此外,我还安装了一个不错的小警报系统,以防电话号码无效。我的问题是我说得对,但号码有空格和不需要的字符,例如(“123 456-7890”)。UIApplication 仅在您的号码是(“1234567890”)时才有效或接受。因此,您基本上可以通过创建一个新变量来仅提取数字来删除空格和无效字符。然后使用 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()
        }
        }

回答by LuAndre

Swift 3, iOS 10

斯威夫特 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)
            }
        }
  }

回答by Ayath Khan

I am using this method in my application and it's working fine. I hope this may help you too.

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

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

回答by Venk

In Swift 3,

在斯威夫特 3 中,

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

回答by Emmett

I am using swift 3 solution with number validation

我正在使用带有数字验证的 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)")!)
    }