Answers:
mymobileNO.titleLabel.text值可能不包含方案tel://
您的代码应如下所示:
NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
要返回原始应用,您可以使用telprompt://而不是tel://-提示提示符将首先提示用户,但是当通话结束时,它将返回您的应用:
NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
tel://
不telprompt://
合并@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上拨打电话。
迅捷版本:
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.
}
这里的答案是完美的工作。我只是将Craig Mellon的答案转换为Swift。如果有人来寻找快速答案,这将对他们有所帮助。
var phoneNumber: String = "telprompt://".stringByAppendingString(titleLabel.text!) // titleLabel.text has the phone number.
UIApplication.sharedApplication().openURL(NSURL(string:phoneNumber)!)
在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")
}
}
if let url = NSURL(string: "tel://\(number)"),
UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
不建议使用“ openURL:”:在iOS 10.0中首先不建议使用-请改用openURL:options:completionHandler:
在Objective-c iOS 10+中使用:
NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber] options:@{} completionHandler:nil];