在 iOS 中以编程方式使用访问代码拨打电话号码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5456395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dial a phone number with an access code programmatically in iOS
提问by Shahid Aslam
How can I dial a phone number that includes a number and access code programmatically in iOS?
如何在 iOS 中以编程方式拨打包含号码和访问代码的电话号码?
For example:
例如:
number: 900-3440-567
Access Code: 65445
号码:900-3440-567
接入码:65445
回答by vijay adhikari
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];
}
回答by saadnib
follow the tutorial
按照教程
to call a number use -
拨打号码使用 -
NSURL *url = [NSURL URLWithString:@"tel://012-4325-234"];
[[UIApplication sharedApplication] openURL:url];
to open your app after call finished use -
通话完毕后打开您的应用程序 -
(Note: telprompt is undocumented)
(注意:telprompt 没有记录)
NSURL *url = [NSURL URLWithString:@"telprompt://012-4325-234"];
[[UIApplication sharedApplication] openURL:url];
回答by Chris Doble
You can programmatically dial phone numbers using UIApplication
's openURL:
method (see example below). I'm unsure if access codes are supported, but this is at least a starting point.
您可以使用UIApplication
的openURL:
方法以编程方式拨打电话号码(请参见下面的示例)。我不确定是否支持访问代码,但这至少是一个起点。
NSURL *URL = [NSURL URLWithString:@"tel://900-3440-567"];
[[UIApplication sharedApplication] openURL:URL];
Edit:See the Apple URL Scheme Referenceand the UIApplication Class Referencefor more information.
编辑:有关更多信息,请参阅Apple URL Scheme Reference和UIApplication Class Reference。
回答by rubybeginner
I don't know if you actually found a solution for passing the access code, but for me this code worked:
我不知道您是否真的找到了传递访问代码的解决方案,但对我而言,此代码有效:
NSString *dialstring = [[NSString alloc] initWithFormat:@"tel:your_phonenumber,your_accessnumber"];
That will result in a dial string with the following values:
tel:9003440567,65445
这将产生具有以下值的拨号字符串:
tel:9003440567,65445
The remaining parts are managed by the phone app of iOS with the following command:
其余部分由 iOS 的手机应用程序使用以下命令进行管理:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:dialstring]];
The ,
in the string causes a pause in your telephonesystem (the one where you want to access a conference room) right after the first number is dialed and a connection is established. So the telephonesystem has time to ask you for the access code (I think it should ask you, that's the way our system works). And after that your access code should be passed.
该,
字符串中导致您telephonesystem暂停第拨打该号码,并建立连接后右(您要访问的会议室中的一个)。所以telephonesystem有时间问你的访问代码(我想应该问你,这是我们的系统的工作方式)。之后你的访问代码应该被传递。
BE AWARE: Your access code will be passed in in a non-secret way. For example: Your shown access code will be displayed in the iPhone phone app display this way: 9003440567, 65445
请注意:您的访问代码将以非秘密方式传递。例如:您显示的访问代码将在 iPhone 手机应用程序中以这种方式显示:9003440567、65445
回答by user2637900
Using this user can redirect on Call and after the call he/she will automatically redirected to the app. It's working for me and sure about it.
使用此用户可以重定向呼叫,呼叫后他/她将自动重定向到应用程序。它对我有用,并且可以肯定。
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];
}
回答by Zorayr
Here is a self-contained solution in Swift:
这是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);
}
}
}
Now you should be able to use callNumber("7178881234")
to make a call; hope this helps!
现在您应该可以使用callNumber("7178881234")
拨打电话了;希望这可以帮助!
回答by carmen_munich
It's not possible to dial programmatically a phone number that includes number and access code.
无法以编程方式拨打包含号码和访问代码的电话号码。
The Apple Developer Library gives the following info:
Apple Developer Library 提供以下信息:
"...the Phone application supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone application does not attempt to dial the corresponding phone number."
“...电话应用程序支持电话方案中的大部分(但不是全部)特殊字符。具体来说,如果 URL 包含 * 或# 字符,电话应用程序不会尝试拨打相应的电话号码。”
回答by Hemant
You can use Phone urls to invoke the Phone application to dial a number for you. See this reference.
您可以使用电话 URL 调用电话应用程序为您拨打号码。请参阅此参考。
The downside is that once the call is finished, user will endup in the Phone application. But I am afraid there is no solution to that problem. iOS doesn't allow any application to directly initiate a call because of security and privacy reasons.
缺点是一旦通话结束,用户将在电话应用程序中结束。但恐怕没有办法解决这个问题。由于安全和隐私原因,iOS 不允许任何应用程序直接发起呼叫。
You can use comma for introducing pause(s) while dialing a number.
您可以在拨号时使用逗号来引入暂停。
回答by graiz
There are a number of ways to dial a phone number and the way described that uses:
有多种拨打电话号码的方式以及所描述的使用方式:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:555-555-5555"]
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:555-555-5555"]
Is a valid way to do this however it has a number of issues. First it doesn't properly prompt the user and secondly it doesn't bring the user back to the application when the phone call is completed. To properly place a phone call you should both prompt before the call so you don't surprise the user and you should bring the user back to the application once the call is done.
是一种有效的方法,但是它有很多问题。首先,它没有正确提示用户,其次,当电话呼叫完成时,它没有将用户带回应用程序。要正确拨打电话,您应该在通话前进行提示,以免让用户感到惊讶,并且应该在通话结束后将用户带回应用程序。
Both of these can be accomplished without using a private API as is suggested by some of the answers here. The recommended approach uses the telprompt api but it doesn't use the private instantiation of the call and instead creates a web view allowing for future compatibility.
正如此处的一些答案所建议的那样,这两种方法都可以在不使用私有 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]];
}
A sample project and additional information is provided here: http://www.raizlabs.com/dev/2014/04/getting-the-best-behavior-from-phone-call-requests-using-tel-in-an-ios-app/
此处提供了示例项目和其他信息:http: //www.raizlabs.com/dev/2014/04/getting-the-best-behavior-from-phone-call-requests-using-tel-in-an- ios应用程序/