以编程方式进行电话呼叫

141

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

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

2
对于Swift代码,您可以参考以下答案: https://dev59.com/vGAf5IYBdhLWcg3w0VUf#29869456 - Sahil Kapoor
13个回答

219

要返回原始应用程序,您可以使用telprompt://而不是tel:// - tell prompt将首先提示用户,但当通话结束时,它将返回到您的应用程序:

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

10
我有一个问题:使用telprompt://的应用程序是否会被苹果拒绝? - Smeegol
8
我知道这个已经过时了,但我找到了一些人声称他们的应用程序使用telprompt被批准了: “我提交了我的应用程序,使用了不带uiwebkit的共享应用程序,并已成功通过苹果审核。 回答于2013年1月19日 Pablo Alejandro Junge” - Mark McCorkle
2
telprompt://是未记录的,因此永远不应该依赖它。事情可能会发生变化,例如苹果可能决定某个URL方案是他们需要独占的,或者仅仅是不希望允许,那么你的应用程序将会出现问题。 - DevC
@DevC:那么我们的应用程序中有电话功能的替代方案是什么? - BaSha
@BaSha 使用 tel:// 而不是 telprompt:// - DevC
不需要使用反斜杠,我们可以使用“tel:”和“telprompt:”。 - Durai Amuthan.H

192

很可能 mymobileNO.titleLabel.text 的值不包含方案//

您的代码应该像这样:

ObjectiveC

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

Swift

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

3
用这种方法,我们没有机会回到原始应用程序。 - Artem Oboturov
4
我认为Mellon的方法更好,因为它首先提示用户,然后在通话后返回到您的应用程序。只需确保使用canOpenURL:并回退到tel:提示,因为telprompt不是官方的,可能会在将来的iOS版本中被删除。 - joel.d
我该如何从Apple Watch打电话。 - Ahad Khan
1
我可以在iOS中以编程方式为这种方式发起的通话打开扬声器吗? - uniruddh
甚至 tel:1234567890 也可以像 tell://1234567890 一样工作。 - Durai Amuthan.H

25

结合@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.
}

10

这里的答案完全有效。我只是将Craig Mellon的答案转换为Swift语言。如果有人在寻找Swift语言的答案,这会对他们有所帮助。

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

你可以在telprompt://的位置使用tel://。 - Kundan
let phoneNumber: String = "telprompt://(titleLabel.text)" UIApplication.shared.openURL(URL(string:phoneNumber)!) - tspentzas

9

如果您正在使用Xamarin开发iOS应用程序,这里是在应用程序中拨打电话的相应C#等效代码:

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

6

Swift 3

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

4

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

    }

3
Java RoboVM的等价物:
public void dial(String number)
{
  NSURL url = new NSURL("tel://" + number);
  UIApplication.getSharedApplication().openURL(url);
}

3
尝试了上面的Swift 3选项,但它没有起作用。如果你想在Swift 3上针对iOS 10+运行,我认为你需要以下内容: Swift 3(iOS 10+):
let phoneNumber = mymobileNO.titleLabel.text       
UIApplication.shared.open(URL(string: phoneNumber)!, options: [:], completionHandler: nil)

2

Swift

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接