ios 以编程方式拨打电话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4929717/
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
Make a phone call programmatically
提问by user564963
How can I make a phone call programmatically on iPhone? I tried the following code but nothing happened:
如何在 iPhone 上以编程方式拨打电话?我尝试了以下代码,但没有任何反应:
NSString *phoneNumber = mymobileNO.titleLabel.text;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
回答by Craig Mellon
To go back to original app you can use telprompt:// instead of tel:// - The tell prompt will prompt the user first, but when the call is finished it will go back to your app:
要返回原始应用程序,您可以使用 telprompt:// 而不是 tel:// - tell 提示将首先提示用户,但当呼叫完成后,它将返回到您的应用程序:
NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
回答by Cristian Radu
Probably the mymobileNO.titleLabel.textvalue doesn't include the scheme tel://
可能mymobileNO.titleLabel.text值不包括方案tel://
Your code should look like this:
您的代码应如下所示:
NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
回答by Guillaume Boudreau
Merging the answers of @Cristian Radu and @Craig Mellon, and the comment from @joel.d, you should do:
合并@Cristian Radu 和@Craig Mellon 的答案,以及@joel.d 的评论,你应该这样做:
NSURL *urlOption1 = [NSURL URLWithString:[@"telprompt://" stringByAppendingString:phone]];
NSURL *urlOption2 = [NSURL URLWithString:[@"tel://" stringByAppendingString:phone]];
NSURL *targetURL = nil;
if ([UIApplication.sharedApplication canOpenURL:urlOption1]) {
targetURL = urlOption1;
} else if ([UIApplication.sharedApplication canOpenURL:urlOption2]) {
targetURL = urlOption2;
}
if (targetURL) {
if (@available(iOS 10.0, *)) {
[UIApplication.sharedApplication openURL:targetURL options:@{} completionHandler:nil];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[UIApplication.sharedApplication openURL:targetURL];
#pragma clang diagnostic pop
}
}
This will first try to use the "telprompt://" URL, and if that fails, it will use the "tel://" URL. If both fails, you're trying to place a phone call on an iPad or iPod Touch.
这将首先尝试使用“telprompt://” URL,如果失败,它将使用“tel://” URL。如果两者都失败,则您正在尝试在 iPad 或 iPod Touch 上拨打电话。
Swift Version :
快速版本:
let phone = mymobileNO.titleLabel.text
let phoneUrl = URL(string: "telprompt://\(phone)"
let phoneFallbackUrl = URL(string: "tel://\(phone)"
if(phoneUrl != nil && UIApplication.shared.canOpenUrl(phoneUrl!)) {
UIApplication.shared.open(phoneUrl!, options:[String:Any]()) { (success) in
if(!success) {
// Show an error message: Failed opening the url
}
}
} else if(phoneFallbackUrl != nil && UIApplication.shared.canOpenUrl(phoneFallbackUrl!)) {
UIApplication.shared.open(phoneFallbackUrl!, options:[String:Any]()) { (success) in
if(!success) {
// Show an error message: Failed opening the url
}
}
} else {
// Show an error message: Your device can not do phone calls.
}
回答by Shivaay
The answers here are perfectly working. I am just converting Craig Mellon answer to Swift. If someone comes looking for swift answer, this will help them.
这里的答案非常有效。我只是将 Craig Mellon 的答案转换为 Swift。如果有人来寻求快速答复,这将对他们有所帮助。
var phoneNumber: String = "telprompt://".stringByAppendingString(titleLabel.text!) // titleLabel.text has the phone number.
UIApplication.sharedApplication().openURL(NSURL(string:phoneNumber)!)
回答by Michael Kniskern
If you are using Xamarin to develop an iOS application, here is the C# equivalent to make a phone call within your application:
如果您使用 Xamarin 开发 iOS 应用程序,这里是在您的应用程序中拨打电话的等效 C#:
string phoneNumber = "1231231234";
NSUrl url = new NSUrl(string.Format(@"telprompt://{0}", phoneNumber));
UIApplication.SharedApplication.OpenUrl(url);
回答by Jorge Costa
Swift 3
斯威夫特 3
let phoneNumber: String = "tel://3124235234"
UIApplication.shared.openURL(URL(string: phoneNumber)!)
回答by Rohit Pathak
In Swift 3.0,
在 Swift 3.0 中,
static func callToNumber(number:String) {
let phoneFallback = "telprompt://\(number)"
let fallbackURl = URL(string:phoneFallback)!
let phone = "tel://\(number)"
let url = URL(string:phone)!
let shared = UIApplication.shared
if(shared.canOpenURL(fallbackURl)){
shared.openURL(fallbackURl)
}else if (shared.canOpenURL(url)){
shared.openURL(url)
}else{
print("unable to open url for call")
}
}
回答by Charlie Seligman
Tried the Swift 3 option above, but it didnt work. I think you need the following if you are to run against iOS 10+ on Swift 3:
尝试了上面的 Swift 3 选项,但没有用。如果您要在 Swift 3 上针对 iOS 10+ 运行,我认为您需要以下内容:
Swift 3 (iOS 10+):
斯威夫特 3(iOS 10+):
let phoneNumber = mymobileNO.titleLabel.text
UIApplication.shared.open(URL(string: phoneNumber)!, options: [:], completionHandler: nil)
回答by EntangledLoops
The Java RoboVM equivalent:
Java RoboVM 等效项:
public void dial(String number)
{
NSURL url = new NSURL("tel://" + number);
UIApplication.getSharedApplication().openURL(url);
}
回答by Himali Shah
let phone = "tel://\("1234567890")";
let url:NSURL = NSURL(string:phone)!;
UIApplication.sharedApplication().openURL(url);