如何在iPhone上以编程方式拨打电话?我尝试了以下代码,但什么也没有发生:
NSString *phoneNumber = mymobileNO.titleLabel.text;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
发布于 2012-04-10 20:33:54
要返回原始应用程序,您可以使用telprompt://而不是tel:// - tell提示符将首先提示用户,但当调用完成后,它将返回到您的应用程序:
NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
发布于 2015-10-28 22:04:43
合并@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
}
}
这将首先尝试使用"telprompt://“URL,如果失败,将使用"tel://”URL。如果两者都失败了,您将尝试在iPad或iPod Touch上拨打电话。
Swift版本:
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.
}
发布于 2015-09-15 22:41:13
这里的答案是完美的。我只是将Craig Mellon answer转换为Swift。如果有人来寻求快速的答案,这将帮助他们。
var phoneNumber: String = "telprompt://".stringByAppendingString(titleLabel.text!) // titleLabel.text has the phone number.
UIApplication.sharedApplication().openURL(NSURL(string:phoneNumber)!)
https://stackoverflow.com/questions/4929717
复制相似问题