Make a phone call programmatically

IosObjective CIphoneTelephonyPhone Call

Ios Problem Overview


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]];

Ios Solutions


Solution 1 - Ios

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]];

Solution 2 - Ios

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

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

Solution 3 - Ios

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.
}

Solution 4 - Ios

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

Solution 5 - Ios

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

Solution 6 - Ios

Swift 3

let phoneNumber: String = "tel://3124235234"
UIApplication.shared.openURL(URL(string: phoneNumber)!)

Solution 7 - Ios

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")
        }

    }

Solution 8 - Ios

The Java RoboVM equivalent:

public void dial(String number)
{
  NSURL url = new NSURL("tel://" + number);
  UIApplication.getSharedApplication().openURL(url);
}

Solution 9 - Ios

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)

Solution 10 - Ios

let phone = "tel://\("1234567890")";
let url:NSURL = NSURL(string:phone)!;
UIApplication.sharedApplication().openURL(url);

Solution 11 - Ios

'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];

Solution 12 - Ios

Swift

if let url = NSURL(string: "tel://\(number)"), 
    UIApplication.sharedApplication().canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

Solution 13 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser564963View Question on Stackoverflow
Solution 1 - IosCraig MellonView Answer on Stackoverflow
Solution 2 - IosCristian RaduView Answer on Stackoverflow
Solution 3 - IosGuillaume BoudreauView Answer on Stackoverflow
Solution 4 - IosKundanView Answer on Stackoverflow
Solution 5 - IosMichael KniskernView Answer on Stackoverflow
Solution 6 - IosJorge CostaView Answer on Stackoverflow
Solution 7 - IosRohit PathakView Answer on Stackoverflow
Solution 8 - IosEntangledLoopsView Answer on Stackoverflow
Solution 9 - IosCharlie SeligmanView Answer on Stackoverflow
Solution 10 - IosHimali ShahView Answer on Stackoverflow
Solution 11 - IosBosoudView Answer on Stackoverflow
Solution 12 - IosSazzad Hissain KhanView Answer on Stackoverflow
Solution 13 - IosNDCoderView Answer on Stackoverflow