在iOS中以编程方式拨打带有访问代码的电话号码

时间:2011-03-28 08:28:57

标签: ios iphone ios4 ios-simulator

如何在iOS中以编程方式拨打包含号码和访问代码的电话号码?

例如:

  

号码:900-3440-567
  访问代码:65445

10 个答案:

答案 0 :(得分:107)

UIDevice *device = [UIDevice currentDevice];
if ([[device model] isEqualToString:@"iPhone"] ) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:130-032-2837"]]];
} else {
    UIAlertView *notPermitted=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Your device doesn't support this feature." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [notPermitted show];
    [notPermitted release];
}

答案 1 :(得分:34)

按照教程

http://www.makebetterthings.com/blogs/iphone/open-phone-sms-email-map-and-browser-apps-in-iphone-sdk/

拨打号码 -

NSURL *url = [NSURL URLWithString:@"tel://012-4325-234"];
[[UIApplication sharedApplication] openURL:url];

在通话结束后打开您的应用 -

(注意:telprompt未记录)

NSURL *url = [NSURL URLWithString:@"telprompt://012-4325-234"];
[[UIApplication sharedApplication] openURL:url];

答案 2 :(得分:19)

您可以使用UIApplication的{​​{1}}方法以编程方式拨打电话号码(请参阅下面的示例)。我不确定是否支持访问代码,但这至少是一个起点。

openURL:

修改:有关详细信息,请参阅Apple URL Scheme ReferenceUIApplication Class Reference

答案 3 :(得分:7)

我不知道你是否真的找到了传递访问代码的解决方案,但对我来说这个代码有效:

NSString *dialstring = [[NSString alloc] initWithFormat:@"tel:your_phonenumber,your_accessnumber"]; 

这将导致拨号字符串具有以下值: tel:9003440567,65445

其余部分由iOS的手机应用程序使用以下命令进行管理:

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

在拨打第一个号码并建立连接后,字符串中的,会导致电话系统(您要访问会议室的电话系统)暂停。因此,电话系统有时间向您询问访问代码(我认为应该问您,这就是我们系统的工作方式)。之后,您的访问代码应该被传递。

BE AWARE :您的访问代码将以非秘密方式传入。例如:您显示的访问代码将以这种方式显示在iPhone手机应用程序中: 9003440567,65445

答案 4 :(得分:3)

使用此用户可以重定向呼叫,呼叫后他/她将自动重定向到应用。它对我有用并且确定无疑。

if ([[device model] isEqualToString:@"iPhone"] ) {

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

} else {

    UIAlertView *warning =[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Your device doesn't support this feature." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [warning show];
}

答案 5 :(得分:3)

以下是 Swift

中的独立解决方案
private func callNumber(phoneNumber:String) {
  if let phoneCallURL:NSURL = NSURL(string: "tel://\(phoneNumber)") {
    let application:UIApplication = UIApplication.sharedApplication()
    if (application.canOpenURL(phoneCallURL)) {
      application.openURL(phoneCallURL);
    }
  }
}

现在您应该可以使用callNumber("7178881234")拨打电话;希望这有帮助!

答案 6 :(得分:2)

您可以使用电话网址来调用电话应用程序为您拨打电话号码。请参阅此reference

缺点是呼叫完成后,用户将在电话应用程序中结束。但我担心这个问题没有解决办法。由于安全和隐私原因,iOS不允许任何应用程序直接发起呼叫。

您可以使用逗号在拨号时引入暂停。

答案 7 :(得分:2)

无法以编程方式拨打包含号码和访问代码的电话号码。

Apple Developer Library提供以下信息:

“...电话应用程序支持tel方案中的大多数但不是全部特殊字符。具体来说,如果URL包含*或#characters ,则电话应用程序不会尝试拨打相应的电话号码。“

请参阅:Apple URL Scheme Reference

答案 8 :(得分:1)

有多种拨打电话号码的方式和使用的方式:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@“tel:555-555-5555”]

这是一种有效的方法,但它存在许多问题。首先,它没有正确地提示用户,其次,当电话呼叫完成时,它不会将用户带回应用程序。要正确拨打电话,您应该在通话前提示,这样您就不会对用户感到惊讶,并且您应该在通话结束后将用户带回应用程序。

这些都可以在不使用私有API的情况下完成,如此处的一些答案所示。建议的方法使用telprompt api,但它不使用调用的私有实例化,而是创建一个Web视图,以便将来兼容。

+ (void)callWithString:(NSString *)phoneString
{
    [self callWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneString]]];
}
+ (void)callWithURL:(NSURL *)url
{
    static UIWebView *webView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{  
        webView = [UIWebView new];
    });
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
}

此处提供了一个示例项目和其他信息: http://www.raizlabs.com/dev/2014/04/getting-the-best-behavior-from-phone-call-requests-using-tel-in-an-ios-app/

答案 9 :(得分:0)

Swift 5.0: 如果有人需要更新的快速代码:

private func callNumber(phoneNumber:String) {
    if let phoneCallURL:NSURL = NSURL(string: "tel://\(phoneNumber)") {
        let application:UIApplication = UIApplication.shared
        if (application.canOpenURL(phoneCallURL as URL)) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(phoneCallURL as URL)
            } else {
                UIApplication.shared.openURL(phoneCallURL as URL)
            }
        }
    }
}