44

I have converted the code for making a phone call from Objective-C to Swift, but in Objective-C, we can set the type of the URL that we like to open (e.g. telephone, SMS, web) like this:

@"tel:xx"
@"mailto:info@example.es"
@"http://stackoverflow.com"
@"sms:768number"

The code in Swift is:

UIApplication.sharedApplication().openURL(NSURL(string : "9809088798")

I read that have not released any scheme parameter for tel:, but I don't know if Swift can detect if the string is for making a phone call, sending email, or opening a website. Or may I write:

(string : "tel//:9809088798")

?

Pang
  • 8,605
  • 144
  • 77
  • 113
user3739367
  • 3,591
  • 6
  • 16
  • 17
  • I don't see what the problem is. Did you try it? Did it work? And please do not delete and repost questions. – Mick MacCallum Jun 16 '14 at 20:04
  • 1
    My problem is that I want know if this is correct for call: UIApplication.sharedApplication().openURL(NSURL(string : "9809088798") Because have only the emulator for try it – user3739367 Jun 16 '14 at 20:06

12 Answers12

82

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

Lou Franco
  • 83,503
  • 14
  • 127
  • 183
  • 1
    Just a tiny correction UIApplication.sharedApplication().openURL(NSURL(string: "tel://9809088798")!) you need to use the ! after the NSURL because it returns an optional. – brduca Aug 06 '15 at 11:21
  • this will directly start calling on the number (ofcourse after 10.3 iOS will ask permission to call) but what if I need to open the iPhone dialer with filled a number ?? Can I achieve this? – Suryakant Sharma Dec 11 '17 at 12:18
  • Awesome Answer sir :) – steveSarsawa Jul 23 '19 at 08:56
19

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.

Zorayr
  • 20,232
  • 5
  • 111
  • 102
  • The problem is that your solution does not return to the app after the phone call has been finished on iOS7. – confile Jun 19 '15 at 13:50
  • Hm, curious if there is a solution that does do that.. might be a restriction on iOS. – Zorayr Jun 19 '15 at 19:12
8

For Swift in iOS:

var url:NSURL? = NSURL(string: "tel://9809088798")
UIApplication.sharedApplication().openURL(url!)
Pang
  • 8,605
  • 144
  • 77
  • 113
Vinod Joshi
  • 7,094
  • 46
  • 49
  • this will directly start calling on the number (ofcourse after 10.3 iOS will ask permission to call) but what if I need to open the iPhone dialer with filled a number ?? Can I achieve this? – Suryakant Sharma Dec 11 '17 at 12:18
7

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.

Robert Wagstaff
  • 2,594
  • 1
  • 24
  • 40
6

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

Glenn
  • 2,650
  • 1
  • 21
  • 29
5

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

Small update to Swift 3

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

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  
    }
Durai Amuthan.H
  • 28,889
  • 6
  • 148
  • 223
1

For making a call in swift 5.1, just use the following code: (I have tested it in Xcode 11)

let phone = "1234567890"
if let callUrl = URL(string: "tel://\(phone)"), UIApplication.shared.canOpenURL(callUrl) {
     UIApplication.shared.open(callUrl)
}

Edit: For Xcode 12.4, swift 5.3, just use the following:

UIApplication.shared.open(NSURL(string: "tel://555-123-1234")! as URL)

Make sure that you import UIKit, or it will say that it cannot find UIApplication in scope.

NDCoder
  • 29
  • 1
  • 5
0

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);
            }
        }
Arshad
  • 886
  • 7
  • 12
0

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)
Nil
  • 119
  • 3
  • 11
0

Swift 4 and above

let dialer = URL(string: "tel://5028493750")
    if let dialerURL = dialer {
        UIApplication.shared.open(dialerURL)
}
Akbar Khan
  • 1,442
  • 13
  • 17