如何以编程方式拨打电话?

时间:2011-01-03 05:52:08

标签: iphone objective-c ios

我需要通过按钮点击在我的应用程序中以编程方式调用。

为此我找到了这样的代码。

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

它是否适用于iphone sdk 3.0和iphone 2.0

可以帮忙吗

提前感谢你。

3 个答案:

答案 0 :(得分:48)

将电话号码保存在单独的字符串中。

NSString *phoneNumber = @"1-800-555-1212"; // dynamically assigned
NSString *phoneURLString = [NSString stringWithFormat:@"tel:%@", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];

答案 1 :(得分: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

答案 2 :(得分:0)

以下代码段会检查SIM卡是否存在,如果设备能够拨打电话,例如非SIM卡设备

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

不要忘记添加CoreTelephony框架

Credit