47

How can I dial a phone number that includes a number and access code programmatically in iOS?

For example:

number: 900-3440-567
Access Code: 65445

Cœur
  • 37,241
  • 25
  • 195
  • 267
Shahid Aslam
  • 2,585
  • 3
  • 24
  • 31
  • possible duplicate of [Programatically Dial a Phone number and pass DTMF using the iPhone SDK](http://stackoverflow.com/questions/584663/programatically-dial-a-phone-number-and-pass-dtmf-using-the-iphone-sdk) – Senseful Jun 19 '15 at 21:46

10 Answers10

105
UIDevice *device = [UIDevice currentDevice];
if ([[device model] isEqualToString:@"iPhone"] ) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:130-032-2837"]]];
} else {
    UIAlertView *notPermitted=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Your device doesn't support this feature." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [notPermitted show];
    [notPermitted release];
}
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
vijay adhikari
  • 2,455
  • 1
  • 16
  • 22
  • 5
    thanks guys for posting code it really helps but what about the Access Code how to dial this automatically for a meeting ? – Shahid Aslam Mar 28 '11 at 13:14
  • 7
    You should be using canOpenURL instead of device model. Remember that iPad and iPod touch can make calls to FaceTime numbers. http://blog.mugunthkumar.com/coding/iphone-tutorial-better-way-to-check-capabilities-of-ios-devices/ – Mugunth Sep 18 '12 at 01:58
  • 5
    Really great thanks. Would have been more great if you had named the UIAlertView instance with a lower case though – Adam Waite Feb 14 '13 at 17:41
  • 1
    Minor note: I believe the URL should begin "tel://" as suggested in various other answers. – Matt May 09 '14 at 05:43
  • `openURL` is now deprecated, and so is `UIAlertView` – Jean-Denis Muys Nov 11 '18 at 11:10
  • Make sure to URL encode the number to make sure it works with `()` and spaces. – Rivera May 18 '20 at 22:10
33

follow the tutorial

http://www.makebetterthings.com/blogs/iphone/open-phone-sms-email-map-and-browser-apps-in-iphone-sdk/

to call a number use -

NSURL *url = [NSURL URLWithString:@"tel://012-4325-234"];
[[UIApplication sharedApplication] openURL:url];

to open your app after call finished use -

(Note: telprompt is undocumented)

NSURL *url = [NSURL URLWithString:@"telprompt://012-4325-234"];
[[UIApplication sharedApplication] openURL:url];
saadnib
  • 11,145
  • 2
  • 33
  • 54
  • 1
    Isn't telprompt:// a private method that would be rejected by Apple? – Smeegol Jul 24 '12 at 01:46
  • If it does not work, is it either because it is an iPad or telephone number is invalid (make sure it does not contain spaces). – Legoless Jul 26 '13 at 02:11
  • @saadnib Avoid using telprompt. Since Apple does not have documentation on telprompt, its functionality may be removed without notice (low chance though). https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/PhoneLinks/PhoneLinks.html#//apple_ref/doc/uid/TP40007899-CH6-SW1 – Gaurav Borole Jun 08 '16 at 07:03
19

You can programmatically dial phone numbers using UIApplication's openURL: method (see example below). I'm unsure if access codes are supported, but this is at least a starting point.

NSURL *URL = [NSURL URLWithString:@"tel://900-3440-567"];
[[UIApplication sharedApplication] openURL:URL];

Edit: See the Apple URL Scheme Reference and the UIApplication Class Reference for more information.

  • You should check if the device is capable of handling the URL scheme, see `[UIApplication sharedApplication] canOpenURL:...]`. – Zorayr Apr 25 '15 at 18:47
7

I don't know if you actually found a solution for passing the access code, but for me this code worked:

NSString *dialstring = [[NSString alloc] initWithFormat:@"tel:your_phonenumber,your_accessnumber"]; 

That will result in a dial string with the following values: tel:9003440567,65445

The remaining parts are managed by the phone app of iOS with the following command:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:dialstring]];

The , in the string causes a pause in your telephonesystem (the one where you want to access a conference room) right after the first number is dialed and a connection is established. So the telephonesystem has time to ask you for the access code (I think it should ask you, that's the way our system works). And after that your access code should be passed.

BE AWARE: Your access code will be passed in in a non-secret way. For example: Your shown access code will be displayed in the iPhone phone app display this way: 9003440567, 65445

rubybeginner
  • 79
  • 1
  • 5
  • dialstring have to use "{" and "}" -> method [[UIApplication sharedApplication] openURL:[NSURL URLWithString:dialstring]]; not call – vualoaithu Dec 23 '13 at 09:10
3

Using this user can redirect on Call and after the call he/she will automatically redirected to the app. It's working for me and sure about it.

if ([[device model] isEqualToString:@"iPhone"] ) {

    NSString *phoneNumber = [@"telprompt://" stringByAppendingString:cellNameStr];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

} else {

    UIAlertView *warning =[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Your device doesn't support this feature." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [warning show];
}
HRM
  • 2,097
  • 6
  • 23
  • 37
  • Should be using `[UIApplication sharedApplication] canOpenURL:...]` instead of checking if the device is an iPhone. – Zorayr Apr 25 '15 at 18:47
3

Here is 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; hope this helps!

Zorayr
  • 23,770
  • 8
  • 136
  • 129
2

It's not possible to dial programmatically a phone number that includes number and access code.

The Apple Developer Library gives the following info:

"...the Phone application supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone application does not attempt to dial the corresponding phone number."

See: Apple URL Scheme Reference

Community
  • 1
  • 1
Carmen
  • 6,177
  • 1
  • 35
  • 40
2

You can use Phone urls to invoke the Phone application to dial a number for you. See this reference.

The downside is that once the call is finished, user will endup in the Phone application. But I am afraid there is no solution to that problem. iOS doesn't allow any application to directly initiate a call because of security and privacy reasons.

You can use comma for introducing pause(s) while dialing a number.

Hemant
  • 19,486
  • 24
  • 91
  • 127
1

There are a number of ways to dial a phone number and the way described that uses:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:555-555-5555"]

Is a valid way to do this however it has a number of issues. First it doesn't properly prompt the user and secondly it doesn't bring the user back to the application when the phone call is completed. To properly place a phone call you should both prompt before the call so you don't surprise the user and you should bring the user back to the application once the call is done.

Both of these can be accomplished without using a private API as is suggested by some of the answers here. The recommended approach uses the telprompt api but it doesn't use the private instantiation of the call and instead creates a web view allowing for future compatibility.

+ (void)callWithString:(NSString *)phoneString
{
    [self callWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneString]]];
}
+ (void)callWithURL:(NSURL *)url
{
    static UIWebView *webView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{  
        webView = [UIWebView new];
    });
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
}

A sample project and additional information is provided here: http://www.raizlabs.com/dev/2014/04/getting-the-best-behavior-from-phone-call-requests-using-tel-in-an-ios-app/

graiz
  • 71
  • 5
  • Note the original question asked how to dial both a number as well as an access code. The access code can be dialed by using a pause sequence as well as the access code. The pause is done with "555-1212,65455" Note that each comma usually represents about a two second pause. – graiz Apr 08 '14 at 03:48
  • The URL string should start with "tel://" . Whilst it is possible that the parameter phoneString passed in the example may begin with "//" I suspect this isn't the case – Martin Lockett Aug 10 '14 at 13:37
0

Swift 5.0: In case someone needed an updated swift code:

private func callNumber(phoneNumber:String) {
    if let phoneCallURL:NSURL = NSURL(string: "tel://\(phoneNumber)") {
        let application:UIApplication = UIApplication.shared
        if (application.canOpenURL(phoneCallURL as URL)) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(phoneCallURL as URL)
            } else {
                UIApplication.shared.openURL(phoneCallURL as URL)
            }
        }
    }
}
Criss Gibran
  • 104
  • 2
  • 5