11

I need to call programmatically in my app in a button click.

for that i found code like this.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:1-800-555-1212"]];

Is it work in iphone sdk 3.0 and iphone 2.0 also

Can any pls help

Thank u in advance.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Mahesh Babu
  • 3,395
  • 9
  • 48
  • 97

3 Answers3

47

Keep the phone number in a separate string.

NSString *phoneNumber = @"1-800-555-1212"; // dynamically assigned
NSString *phoneURLString = [NSString stringWithFormat:@"tel:%@", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];
John Willemse
  • 6,608
  • 7
  • 31
  • 45
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
  • This only works on iPhone. 3.2 would be iPad, so technically, it will not work on 3.2. – WrightsCS Jan 03 '11 at 06:42
  • 12
    A worthwhile check would be: `if ([UIApplication instancesRespondToSelector:@selector(canOpenURL:)]) { NSURL *aURL = [NSURL URLWithString:@"tel:1234567890"]; if ([[UIApplication sharedApplication] canOpenURL:aURL]) { [[UIApplication sharedApplication] openURL:aURL]; } }` – joshpaul Jan 03 '11 at 07:10
1
  NSLog(@"Phone calling...");

        UIDevice *device = [UIDevice currentDevice];

        NSString *cellNameStr = [NSString stringWithFormat:@"%@",self.tableCellNames[indexPath.row]];

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

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

        } else {

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

            [warning show];
        }

// VKJ

Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51
0

The following code snippet checks if SIM card is present or not as well if the device is capable of making the call such as non-sim ios devices

 #import <CoreTelephony/CTTelephonyNetworkInfo.h>
 #import <CoreTelephony/CTCarrier.h>


    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]]) {
        // Check if iOS Device supports phone calls
        CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
        CTCarrier *carrier = [netInfo subscriberCellularProvider];
        NSString *mnc = [carrier mobileNetworkCode];
        // User will get an alert error when they will try to make a phone call in airplane mode.
        if (([mnc length] == 0)) {
            // Device cannot place a call at this time.  SIM might be removed.
        } else {
            // iOS Device is capable for making calls
        }
    } else {
        // iOS Device is not capable for making calls
    }



    if ( ! [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"sms:"]]) {
       // iOS Device is not capable to send SMS messages. 
    }

Don't forget to add the CoreTelephony framework

Credit

Community
  • 1
  • 1
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241