【问题标题】:How to call a phone number from ios app? [duplicate]如何从 ios 应用程序拨打电话号码? [复制]
【发布时间】:2021-07-18 16:21:11
【问题描述】:

我正在尝试使用以下方式从 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

【问题讨论】:

标签: ios objective-c


【解决方案1】:

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

也试试这个,

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.open(call, options: []) { result in
       // do something with result
    }
  }
}

【讨论】:

  • 谢谢!我尝试使用您提供的电话号码,它可以工作。也许这是我的电话号码格式。用我的我得到“呼叫设施不可用!!!”我的电话号码是否会因为 + 而关闭,例如:+39 0668806972
  • 我的问题是我去掉了空格。然后它起作用了。谢谢!
  • telprompt: 不是官方的,它可能会在未来的版本中被删除,或者您的应用可能会被拒绝。
  • 嘿你不见了 // 在 telprompt:-----应该是 telprompt:// 。 -1 粗心。我花了很长时间才找到问题所在。 :P
【解决方案2】:

电话无法在模拟器/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.
}

【讨论】:

    【解决方案3】:

    使用以下方法拨打电话:

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

    【讨论】:

    • 请注意,telprompt: 不是官方方案,因此可以在以后的版本中从 iOS 中删除。
    【解决方案4】:

    你可以像下面这样尝试。

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

    希望对你有帮助。

    【讨论】:

    • tel:中删除//,它们不在tel URL scheme
    • 我试过了,没有任何反应。该方法与 nslog 输出一起被调用:phone btn touch tel:+39 0668806972
    • 你是在 iPhone 上测试吗?因为这是正确的代码人。
    • @Manthan 是的,在设备上进行测试。我想知道这是否是我的电话号码的格式。我试过 +919876543210 并且它可以工作,但是 +39 0668806972 它没有
    • 嗯所以检查共振峰它不正确。
    【解决方案5】:
     NSString *phoneNumber = [@"tel://" stringByAppendingString:@"9414481799"];
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
    

    这只会在设备上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-03
      • 1970-01-01
      相关资源
      最近更新 更多