以编程方式拨打电话

时间:2011-02-08 04:55:32

标签: ios objective-c iphone telephony phone-call

如何在iPhone上以编程方式拨打电话?我尝试了以下代码但没有发生任何事情:

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

13 个答案:

答案 0 :(得分:217)

要返回原始应用程序,您可以使用telprompt://而不是tel:// - tell提示符将首先提示用户,但是当调用完成后,它将返回到您的应用程序:

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

答案 1 :(得分:189)

mymobileNO.titleLabel.text 值可能不包含方案 tel://

您的代码应如下所示:

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

答案 2 :(得分: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
    }
} 

这将首先尝试使用“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.
}

答案 3 :(得分:9)

这里的答案非常有效。我只是将Craig Mellon的回答转换为Swift。如果有人来寻求快速回答,这将有助于他们。

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

答案 4 :(得分:8)

如果您正在使用Xamarin开发iOS应用程序,那么C#就相当于在您的应用程序中拨打电话:

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

答案 5 :(得分:6)

Swift 3

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

答案 6 :(得分: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")
        }

    }

答案 7 :(得分:3)

Java RoboVM等价物:

{{1}}

答案 8 :(得分: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)

答案 9 :(得分:1)

let phone = "tel://\("1234567890")";
let url:NSURL = NSURL(string:phone)!;
UIApplication.sharedApplication().openURL(url);

答案 10 :(得分:0)

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

答案 11 :(得分:0)

迅速

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

答案 12 :(得分:0)

使用 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。