如何通过编程实现拨打电话?

11

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

为此,我找到了以下代码。

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

这个在 iPhone SDK 3.0 和 iPhone 2.0 上是否可行?

可以请任何人帮忙吗?

先谢谢了。

3个回答

47

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

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

这只适用于iPhone。3.2将是iPad,因此从技术上讲,它将无法在3.2上运行。 - WrightsCS
12
一个值得进行的检查是:if ([UIApplication instancesRespondToSelector:@selector(canOpenURL:)]) { NSURL *aURL = [NSURL URLWithString:@"tel:1234567890"]; if ([[UIApplication sharedApplication] canOpenURL:aURL]) { [[UIApplication sharedApplication] openURL:aURL]; } } - joshpaul

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


0
以下代码片段检查SIM卡是否存在,以及设备是否能够进行呼叫,例如非SIM iOS设备。
 #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框架

信用


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接