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

问题描述 投票:47回答:9

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

例如:

号码:900-3440-567 准入代码:65445

ios iphone ios4 ios-simulator
9个回答
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];
}

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];

19
投票

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

NSURL *URL = [NSURL URLWithString:@"tel://900-3440-567"];
[[UIApplication sharedApplication] openURL:URL];

编辑:有关更多信息,请参阅Apple URL Scheme ReferenceUIApplication Class Reference


7
投票

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

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

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

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

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

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

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


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];
}

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")拨打电话;希望这可以帮助!


2
投票

您可以使用电话URL来调用电话应用程序为您拨打电话号码。看到这个reference

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

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


2
投票

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

Apple Developer Library提供以下信息:

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

见:Apple URL Scheme Reference


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/

© www.soinside.com 2019 - 2024. All rights reserved.