use*_*963 137 iphone telephony objective-c phone-call ios
如何在iPhone上以编程方式拨打电话?我尝试了以下代码但没有发生任何事情:
NSString *phoneNumber = mymobileNO.titleLabel.text;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Run Code Online (Sandbox Code Playgroud)
Cra*_*lon 218
要返回原始应用程序,您可以使用telprompt://而不是tel:// - tell提示符将首先提示用户,但是当调用完成后,它将返回到您的应用程序:
NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Run Code Online (Sandbox Code Playgroud)
小智 189
可能mymobileNO.titleLabel.text值不包括方案tel://
您的代码应如下所示:
NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Run Code Online (Sandbox Code Playgroud)
Gui*_*eau 24
合并@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
}
}
Run Code Online (Sandbox Code Playgroud)
这将首先尝试使用"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.
}
Run Code Online (Sandbox Code Playgroud)
这里的答案非常有效.我只是将Craig Mellon的回答转换为Swift.如果有人来寻找快速回答,这将有助于他们.
var phoneNumber: String = "telprompt://".stringByAppendingString(titleLabel.text!) // titleLabel.text has the phone number.
UIApplication.sharedApplication().openURL(NSURL(string:phoneNumber)!)
Run Code Online (Sandbox Code Playgroud)
如果您使用Xamarin开发iOS应用程序,那么C#就等同于在您的应用程序中拨打电话:
string phoneNumber = "1231231234";
NSUrl url = new NSUrl(string.Format(@"telprompt://{0}", phoneNumber));
UIApplication.SharedApplication.OpenUrl(url);
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
let phoneNumber: String = "tel://3124235234"
UIApplication.shared.openURL(URL(string: phoneNumber)!)
Run Code Online (Sandbox Code Playgroud)