如何在iOS 9中打开手机拨号器?

时间:2016-03-04 09:43:35

标签: ios objective-c iphone ios9 openurl

我找到了解决方案,我需要在info.plist中添加一些代码。我是这样做的:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>tel</string>
</array>

仍然没有帮助。  我收到这个错误:

  

“ - canOpenURL:网址失败:”tel:// 4806501708“ - 错误:”这   应用程序不允许查询方案tel“

我打开拨号器的代码:

NSString *phoneNumber = [@"tel://" stringByAppendingString:lblVenPhoneValue.text];
if ([UIApplication.sharedApplication canOpenURL:[NSURL URLWithString:phoneNumber]]) {
        [UIApplication.sharedApplication openURL:[NSURL URLWithString:phoneNumber]];

我需要做什么?
在此先感谢

4 个答案:

答案 0 :(得分:9)

您是在设备上测试吗? ,因为这不适用于模拟器。设备也应该有SIM卡。

确认以上后尝试以下

在info.plist中

<key>LSApplicationQueriesSchemes</key>
<array>
<string>tel</string>
<string>telprompt</string>
</array>

哪里想打开电话拨号器

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",@"digits"]]]; 

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@",@"digits"]]];

答案 1 :(得分:1)

从@“tel://”中删除“//”它应该可以正常工作

NSString *phoneNumber = [@"tel:" stringByAppendingString:lblVenPhoneValue.text];
if ([UIApplication.sharedApplication canOpenURL:[NSURL URLWithString:phoneNumber]]) {
        [UIApplication.sharedApplication openURL:[NSURL URLWithString:phoneNumber]];

要获得更好的检查,您可以使用

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[NSString stringWithFormat:phoneNumber]]])
{
  CTTelephonyNetworkInfo *networkInfo = [CTTelephonyNetworkInfo new];
  CTCarrier *carrier = [networkInfo subscriberCellularProvider];
  NSString *_code = [carrier mobileNetworkCode];
  if(_code)
  {
    [[UIApplication sharedApplication] openURL:phoneNumber]];
  }
  else
  {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"no_sim" message:@"" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
    [alert show];
  }
}
else
{
  UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"alert" message:@"alert_device_not_support" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
  [alert show];
}

答案 2 :(得分:0)

对于Swift 4.0,请使用此(确保您不在模拟器上)         让cleanPhoneNumber = phone.components(separatedBy:CharacterSet.decimalDigits.inverted).joined(separator:“”)         让urlString:String =“ tel:// \(cleanPhoneNumber)”         如果让phoneCallURL = URL(string:urlString){             如果(UIApplication.shared.canOpenURL(phoneCallURL)){                 UIApplication.shared.open(phoneCallURL,选项:[:],completionHandler:无)             }         }

答案 3 :(得分:0)

这在模拟器中将不起作用,但在iPhone设备上可以正常工作。

私有函数makeCall(number:String){

    if let url = URL(string: "tel://\(number)"), UIApplication.shared.canOpenURL(url) {
        if #available(iOS 10, *) {
            UIApplication.shared.open(url)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
}