以编程方式打电话

如何在 iPhone 上编程打电话? 我尝试了以下代码,但什么也没有发生:

NSString *phoneNumber = mymobileNO.titleLabel.text;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
118267 次浏览

可能 文本值不包括方案 //

您的代码应该如下所示:

目标 C

NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

斯威夫特

if let url = URL(string: "tel://\(mymobileNO.titleLabel.text))") {
UIApplication.shared.open(url)
}

要回到原来的应用程序,你可以使用 tell 提示符://而不是 tel://-tell 提示符将首先提示用户,但是当调用结束时,它将返回到你的应用程序:

NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

如果你正在使用 Xamarin 开发一个 iOS 应用程序,这里的 C # 相当于在你的应用程序中打电话:

string phoneNumber = "1231231234";
NSUrl url = new NSUrl(string.Format(@"telprompt://{0}", phoneNumber));
UIApplication.SharedApplication.OpenUrl(url);

Java RoboVM 等价物:

public void dial(String number)
{
NSURL url = new NSURL("tel://" + number);
UIApplication.getSharedApplication().openURL(url);
}

这里的答案非常有效。我只是把克雷格 · 梅隆的答案换成斯威夫特的。如果有人来寻求迅速的答案,这将有助于他们。

 var phoneNumber: String = "telprompt://".stringByAppendingString(titleLabel.text!) // titleLabel.text has the phone number.
UIApplication.sharedApplication().openURL(NSURL(string:phoneNumber)!)

把@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
}
}

这将首先尝试使用“ tel命令://”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.
}
let phone = "tel://\("1234567890")";
let url:NSURL = NSURL(string:phone)!;
UIApplication.sharedApplication().openURL(url);

Swift 3

let phoneNumber: String = "tel://3124235234"
UIApplication.shared.openURL(URL(string: phoneNumber)!)

尝试了上面的 Swift 3选项,但是没有用。如果你想在 Swift 3上与 iOS 10 + 竞争,我认为你需要以下几点:

Swift 3(iOS 10 +) :

let phoneNumber = mymobileNO.titleLabel.text
UIApplication.shared.open(URL(string: phoneNumber)!, options: [:], completionHandler: nil)

在 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")
}


}

‘ 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];

斯威夫特

if let url = NSURL(string: "tel://\(number)"),
UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

用 openurl。

要在 Swift 5.1中打电话,只需使用以下代码: (我已经在 Xcode 11中测试过了)

let phone = "1234567890"
if let callUrl = URL(string: "tel://\(phone)"), UIApplication.shared.canOpenURL(callUrl) {
UIApplication.shared.open(callUrl)
}

编辑: 对于 Xcode 12.4,Swift 5.3,只需使用以下代码:

UIApplication.shared.open(NSURL(string: "tel://555-123-1234")! as URL)

确保您导入了 UIKit,否则它会说在作用域中找不到 UIApplication。