141

How can I make a phone call programmatically on iPhone? I tried the following code but nothing happened:

NSString *phoneNumber = mymobileNO.titleLabel.text;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user564963
  • 2,284
  • 5
  • 24
  • 33

13 Answers13

219

To go back to original app you can use telprompt:// instead of tel:// - The tell prompt will prompt the user first, but when the call is finished it will go back to your app:

NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Craig Mellon
  • 5,399
  • 2
  • 20
  • 25
  • 10
    I have a question: will the apps that use telprompt:// be rejected by Apple? – Smeegol Jul 23 '12 at 08:46
  • 8
    I know this is old but I did find a couple people claiming their apps were approved using telprompt: "I've submited my app that uses telprompt with shared application without the uiwebkit and has been successfully approved by apple. answered January 19, 2013 Pablo Alejandro Junge" – Mark McCorkle May 15 '13 at 14:16
  • 2
    telprompt:// is undocumented and should therefore never be relied upon. Things may changed, For example Apple could decide a given URL scheme is something they need exclusively or simply do not wish to allow, then your app will break. – DevC Jul 10 '14 at 08:31
  • @DevC: Then what is alternative to have phone call feature in our app? – BaSha Jan 21 '15 at 05:46
  • @BaSha use `tel://` not `telprompt://` – DevC Jan 21 '15 at 10:46
  • without backslash also works..i.e we can use **tel:** and **telprompt:** – Durai Amuthan.H Feb 28 '17 at 11:45
192

Probably the mymobileNO.titleLabel.text value doesn't include the scheme //

Your code should look like this:

ObjectiveC

NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

Swift

if let url = URL(string: "tel://\(mymobileNO.titleLabel.text))") {
    UIApplication.shared.open(url)
}
gfpacheco
  • 2,831
  • 2
  • 33
  • 50
Cristian Radu
  • 8,412
  • 2
  • 19
  • 11
25

Merging the answers of @Cristian Radu and @Craig Mellon, and the comment from @joel.d, you should do:

NSURL *urlOption1 = [NSURL URLWithString:[@"telprompt://" stringByAppendingString:phone]];
NSURL *urlOption2 = [NSURL URLWithString:[@"tel://" stringByAppendingString:phone]];
NSURL *targetURL = nil;

if ([UIApplication.sharedApplication canOpenURL:urlOption1]) {
    targetURL = urlOption1;
} else if ([UIApplication.sharedApplication canOpenURL:urlOption2]) {
    targetURL = urlOption2;
}

if (targetURL) {
    if (@available(iOS 10.0, *)) {
        [UIApplication.sharedApplication openURL:targetURL options:@{} completionHandler:nil];
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        [UIApplication.sharedApplication openURL:targetURL];
#pragma clang diagnostic pop
    }
} 

This will first try to use the "telprompt://" URL, and if that fails, it will use the "tel://" URL. If both fails, you're trying to place a phone call on an iPad or iPod Touch.

Swift Version :

let phone = mymobileNO.titleLabel.text
let phoneUrl = URL(string: "telprompt://\(phone)"
let phoneFallbackUrl = URL(string: "tel://\(phone)"
if(phoneUrl != nil && UIApplication.shared.canOpenUrl(phoneUrl!)) {
  UIApplication.shared.open(phoneUrl!, options:[String:Any]()) { (success) in
    if(!success) {
      // Show an error message: Failed opening the url
    }
  }
} else if(phoneFallbackUrl != nil && UIApplication.shared.canOpenUrl(phoneFallbackUrl!)) {
  UIApplication.shared.open(phoneFallbackUrl!, options:[String:Any]()) { (success) in
    if(!success) {
      // Show an error message: Failed opening the url
    }
  }
} else {
    // Show an error message: Your device can not do phone calls.
}
Itachi
  • 5,777
  • 2
  • 37
  • 69
Guillaume Boudreau
  • 2,676
  • 29
  • 27
10

The answers here are perfectly working. I am just converting Craig Mellon answer to Swift. If someone comes looking for swift answer, this will help them.

 var phoneNumber: String = "telprompt://".stringByAppendingString(titleLabel.text!) // titleLabel.text has the phone number.
        UIApplication.sharedApplication().openURL(NSURL(string:phoneNumber)!)
Kundan
  • 3,084
  • 2
  • 28
  • 65
9

If you are using Xamarin to develop an iOS application, here is the C# equivalent to make a phone call within your application:

string phoneNumber = "1231231234";
NSUrl url = new NSUrl(string.Format(@"telprompt://{0}", phoneNumber));
UIApplication.SharedApplication.OpenUrl(url);
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
6

Swift 3

let phoneNumber: String = "tel://3124235234"
UIApplication.shared.openURL(URL(string: phoneNumber)!)
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
Jorge Costa
  • 249
  • 2
  • 6
4

In Swift 3.0,

static func callToNumber(number:String) {

        let phoneFallback = "telprompt://\(number)"
        let fallbackURl = URL(string:phoneFallback)!

        let phone = "tel://\(number)"
        let url = URL(string:phone)!

        let shared = UIApplication.shared

        if(shared.canOpenURL(fallbackURl)){
            shared.openURL(fallbackURl)
        }else if (shared.canOpenURL(url)){
            shared.openURL(url)
        }else{
            print("unable to open url for call")
        }

    }
Rohit Pathak
  • 359
  • 4
  • 16
3

The Java RoboVM equivalent:

public void dial(String number)
{
  NSURL url = new NSURL("tel://" + number);
  UIApplication.getSharedApplication().openURL(url);
}
EntangledLoops
  • 1,951
  • 1
  • 23
  • 36
3

Tried the Swift 3 option above, but it didnt work. I think you need the following if you are to run against iOS 10+ on Swift 3:

Swift 3 (iOS 10+):

let phoneNumber = mymobileNO.titleLabel.text       
UIApplication.shared.open(URL(string: phoneNumber)!, options: [:], completionHandler: nil)
Charlie S
  • 4,366
  • 6
  • 59
  • 97
2

Swift

if let url = NSURL(string: "tel://\(number)"), 
    UIApplication.sharedApplication().canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Community
  • 1
  • 1
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
1
let phone = "tel://\("1234567890")";
let url:NSURL = NSURL(string:phone)!;
UIApplication.sharedApplication().openURL(url);
Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
Himali Shah
  • 181
  • 1
  • 8
1

'openURL:' is deprecated: first deprecated in iOS 10.0 - Please use openURL:options:completionHandler: instead

in Objective-c iOS 10+ use :

NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber] options:@{} completionHandler:nil];
Bosoud
  • 158
  • 4
  • 24
0

Use openurl.

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
  • 93
  • 2
  • 8