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

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


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.


For Swift in iOS:

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