How to use openURL for making a phone call in Swift?

别来无恙 提交于 2019-11-27 07:45:58

I am pretty sure you want:

UIApplication.sharedApplication().openURL(NSURL(string: "tel://9809088798")!)

(note that in your question text, you put tel//:, not tel://).

NSURL's string init expects a well-formed URL. It will not turn a bunch of numbers into a telephone number. You sometimes see phone-number detection in UIWebView, but that's being done at a higher level than NSURL.

EDIT: Added ! per comment below

A self-contained solution in Swift:

private func callNumber(phoneNumber:String) {
  if let phoneCallURL:NSURL = NSURL(string:"tel://\(phoneNumber)") {
    let application:UIApplication = UIApplication.sharedApplication()
    if (application.canOpenURL(phoneCallURL)) {
      application.openURL(phoneCallURL);
    }
  }
}

Now, you should be able to use callNumber("7178881234") to make a call.

Vinod Joshi

For Swift in iOS:

var url:NSURL? = NSURL(string: "tel://9809088798")
UIApplication.sharedApplication().openURL(url!)

You need to remember to remove the whitespaces or it won't work:

if let telephoneURL = NSURL(string: "telprompt://\(phoneNumber.stringByReplacingOccurrencesOfString(" ", withString: ""))") {
        UIApplication.sharedApplication().openURL(telelphoneURL)
    }

"telprompt://" will prompt the user to call or cancel while "tel://" will call directly.

@ confile:

The problem is that your solution does not return to the app after the phone call has been finished on iOS7. – Jun 19 at 13:50

&@ Zorayr

Hm, curious if there is a solution that does do that.. might be a restriction on iOS.

use

UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://9809088798")!)

You will get a prompt to Call/Cancel but it returns to your application. AFAIK there is no way to return (without prompting)

You must insert "+"\ is another way

private func callNumber(phoneNumber:String) {
  if let phoneCallURL:NSURL = NSURL(string:"tel://"+"\(phoneNumber)") {
    let application:UIApplication = UIApplication.sharedApplication()
    if (application.canOpenURL(phoneCallURL)) {
      application.openURL(phoneCallURL);
    }
  }
}

Small update to Swift 3

UIApplication.shared.openURL(NSURL(string: "telprompt://9809088798")! as URL)

The following code snippet can tell if the SIM is there or not and if the device is capable of making the call and if ok then it'll make the call

  var info = CTTelephonyNetworkInfo()
    var carrier = info.subscriberCellularProvider
    if carrier != nil && carrier.mobileNetworkCode == nil || carrier.mobileNetworkCode.isEqual("") {
       //SIM is not there in the phone
    }
    else if UIApplication.sharedApplication().canopenURL(NSURL(string: "tel://9809088798")!)
    {
    UIApplication.sharedApplication().openURL(NSURL(string: "tel://9809088798")!)
    }
    else    
    {
      //Device does not have call making capability  
    }

For swift 3

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

For swift 4:

func call(phoneNumber: String) {
    if let url = URL(string: phoneNumber) {
        if #available(iOS 10, *) {
            UIApplication.shared.open(url, options: [:],
                                      completionHandler: {
                                        (success) in
                                        print("Open \(phoneNumber): \(success)")
            })
        } else {
            let success = UIApplication.shared.openURL(url)
            print("Open \(phoneNumber): \(success)")
        }
    }
} 

Then, use the function:

let phoneNumber = "tel://+132342424"
call(phoneNumber: phoneNumber)

Same Answer 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")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!