如何从ios应用程序拨打电话号码?

时间:2013-09-20 10:09:25

标签: ios objective-c

我正试图通过以下方式拨打ios应用中的电话号码: 它不起作用,虽然该方法被调用:

-(IBAction)callPhone:(id)sender {

        NSString *phoneCallNum = [NSString stringWithFormat:@"tel://%@",listingPhoneNumber ];

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

        NSLog(@"phone btn touch %@", phoneCallNum);
    }

NSLog输出:手机btn touch tel:// + 39 0668806972

5 个答案:

答案 0 :(得分:89)

你的代码是正确的。你检查过真正的设备吗?此功能在模拟器中不起作用。

试试这个,

NSString *phNo = @"+919876543210";
NSURL *phoneUrl = [NSURL URLWithString:[NSString  stringWithFormat:@"telprompt:%@",phNo]];

    if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
        [[UIApplication sharedApplication] openURL:phoneUrl];
    } else
    {
        calert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Call facility is not available!!!" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [calert show];
    }

** Swift 3版本**

if let url = URL(string: "telprompt:\(phoneNumber)") {
  if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.openURL(url)
  }
}

答案 1 :(得分:14)

电话无法在模拟器/ iPod / iPad上运行,您需要在带有活动SIM卡的iPhone上运行该应用程序。

调用电话应用程序的URL方案也是tel:<phone_number>。请参阅Apple docs.

理想情况下,您应检查设备是否具有电话模块,然后执行openURL:呼叫。使用此代码执行检查,

if([[UIApplication sharedApplication] canOpenURL:callUrl]) {
    [[UIApplication sharedApplication] openURL:callUrl];
}
else {
    //Show error message to user, etc.
}

答案 2 :(得分:5)

使用以下方法拨打电话:

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

答案 3 :(得分:0)

您可以尝试以下内容。

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

希望它对你有所帮助。

答案 4 :(得分:0)

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

这只能在设备上运行。