【发布时间】:2011-07-24 07:20:22
【问题描述】:
如何在 iOS 中以编程方式拨打包含号码和访问代码的电话号码?
例如:
号码:900-3440-567
访问代码:65445
【问题讨论】:
标签: ios iphone ios4 ios-simulator
如何在 iOS 中以编程方式拨打包含号码和访问代码的电话号码?
例如:
号码:900-3440-567
访问代码:65445
【问题讨论】:
标签: ios iphone ios4 ios-simulator
您可以使用电话 URL 来调用电话应用程序为您拨打电话。看到这个reference。
缺点是一旦通话结束,用户将进入电话应用程序。但恐怕这个问题没有解决办法。出于安全和隐私原因,iOS 不允许任何应用程序直接发起呼叫。
您可以在拨号时使用逗号来引入暂停。
【讨论】:
按照教程进行操作
拨打号码使用 -
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];
【讨论】:
您可以使用UIApplication 的openURL: 方法以编程方式拨打电话号码(参见下面的示例)。我不确定是否支持访问代码,但这至少是一个起点。
NSURL *URL = [NSURL URLWithString:@"tel://900-3440-567"];
[[UIApplication sharedApplication] openURL:URL];
编辑:请参阅Apple URL Scheme Reference 和UIApplication Class Reference 了解更多信息。
【讨论】:
[UIApplication sharedApplication] canOpenURL:...]。
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];
}
【讨论】:
openURL 现已弃用,UIAlertView 也是如此
无法以编程方式拨打包含号码和访问代码的电话号码。
Apple 开发者库提供以下信息:
"...Phone 应用程序支持 tel 方案中的大部分(但不是全部)特殊字符。具体来说,如果 URL 包含 * 或 # 字符,Phone 应用程序 不尝试拨打相应的电话号码。”
【讨论】:
我不知道您是否真的找到了传递访问代码的解决方案,但对我来说,此代码有效:
NSString *dialstring = [[NSString alloc] initWithFormat:@"tel:your_phonenumber,your_accessnumber"];
这将产生一个具有以下值的拨号字符串:
tel:9003440567,65445
其余部分由 iOS 手机应用管理,命令如下:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:dialstring]];
字符串中的, 在拨打第一个号码并建立连接后立即导致电话系统(您要访问会议室的系统)暂停。所以电话系统有时间向你询问访问代码(我认为它应该询问你,这就是我们系统的工作方式)。之后,您的访问代码应该被传递。
注意:您的访问代码将以非秘密方式传入。例如:您显示的访问代码将在 iPhone 手机应用显示中显示如下:9003440567, 65445
【讨论】:
使用此用户可以在通话时重定向,通话后他/她将自动重定向到应用程序。它对我有用,而且很确定。
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];
}
【讨论】:
[UIApplication sharedApplication] canOpenURL:...]而不是检查设备是否是iPhone。
有多种拨打电话号码的方法以及所描述的使用方法:
[[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/
【讨论】:
这是 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") 拨打电话了;希望这会有所帮助!
【讨论】:
Swift 5.0: 如果有人需要更新的 swift 代码:
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)
}
}
}
}
【讨论】: