Make a phone call programmatically
Asked Answered
F

13

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]];
Freidafreight answered 8/2, 2011 at 4:55 Comment(1)
For code in swift you can follow this answer https://mcmap.net/q/161558/-how-to-use-openurl-for-making-a-phone-call-in-swiftPreece
M
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)
}
Montane answered 8/2, 2011 at 5:4 Comment(5)
There's no chance we could go back to original application with this approach.Prevot
I think Mellon's approach is better as it prompts the user first, and then returns to your app after the call. Just make sure to use canOpenURL: and fallback to the tel: prompt since telprompt is not official and could be removed in a future iOS version.Funnel
How can I make phone call from apple watch.Raynard
Can I turn on speaker programmatically for call originated this way in ios?Sebbie
even tel:1234567890 also works just like how tell://1234567890 it worksMilquetoast
T
218

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]];
Tonsorial answered 10/4, 2012 at 12:33 Comment(6)
I have a question: will the apps that use telprompt:// be rejected by Apple?Udder
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"Beacham
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.Roybal
@DevC: Then what is alternative to have phone call feature in our app?Latticework
@Latticework use tel:// not telprompt://Roybal
without backslash also works..i.e we can use tel: and telprompt:Milquetoast
M
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)
}
Montane answered 8/2, 2011 at 5:4 Comment(5)
There's no chance we could go back to original application with this approach.Prevot
I think Mellon's approach is better as it prompts the user first, and then returns to your app after the call. Just make sure to use canOpenURL: and fallback to the tel: prompt since telprompt is not official and could be removed in a future iOS version.Funnel
How can I make phone call from apple watch.Raynard
Can I turn on speaker programmatically for call originated this way in ios?Sebbie
even tel:1234567890 also works just like how tell://1234567890 it worksMilquetoast
S
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.
}
Seda answered 28/10, 2015 at 14:4 Comment(0)
S
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)!)
Skilful answered 15/9, 2015 at 14:41 Comment(2)
you can also use tel:// in place of telprompt://Skilful
let phoneNumber: String = "telprompt://(titleLabel.text)" UIApplication.shared.openURL(URL(string:phoneNumber)!)Istria
C
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);
Coffeecolored answered 17/9, 2014 at 16:48 Comment(0)
S
6

Swift 3

let phoneNumber: String = "tel://3124235234"
UIApplication.shared.openURL(URL(string: phoneNumber)!)
Seizure answered 17/11, 2016 at 15:20 Comment(0)
S
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")
        }

    }
Seve answered 26/7, 2017 at 12:20 Comment(0)
A
3

The Java RoboVM equivalent:

public void dial(String number)
{
  NSURL url = new NSURL("tel://" + number);
  UIApplication.getSharedApplication().openURL(url);
}
Awlwort answered 7/5, 2015 at 20:11 Comment(0)
O
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)
Oneal answered 7/12, 2016 at 14:41 Comment(0)
E
2

Swift

if let url = NSURL(string: "tel://\(number)"), 
    UIApplication.sharedApplication().canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Edieedification answered 20/5, 2019 at 6:53 Comment(0)
G
1
let phone = "tel://\("1234567890")";
let url:NSURL = NSURL(string:phone)!;
UIApplication.sharedApplication().openURL(url);
Garter answered 27/5, 2016 at 6:57 Comment(0)
C
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];
Commonalty answered 15/12, 2018 at 9:1 Comment(0)
H
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.

Heterochromatic answered 26/4, 2021 at 23:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.