首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >以编程方式进行电话呼叫

以编程方式进行电话呼叫
EN

Stack Overflow用户
提问于 2011-02-08 12:55:32
回答 10查看 116.7K关注 0票数 140

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

代码语言:javascript
复制
NSString *phoneNumber = mymobileNO.titleLabel.text;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
EN

回答 10

Stack Overflow用户

发布于 2012-04-10 20:33:54

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

代码语言:javascript
复制
NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
票数 219
EN

Stack Overflow用户

发布于 2015-10-28 22:04:43

合并@Cristian Radu和@Craig Mellon的答案,以及@joel.d的评论,你应该这样做:

代码语言:javascript
复制
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版本:

代码语言:javascript
复制
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.
}
票数 25
EN

Stack Overflow用户

发布于 2015-09-15 22:41:13

这里的答案是完美的。我只是将Craig Mellon answer转换为Swift。如果有人来寻求快速的答案,这将帮助他们。

代码语言:javascript
复制
 var phoneNumber: String = "telprompt://".stringByAppendingString(titleLabel.text!) // titleLabel.text has the phone number.
        UIApplication.sharedApplication().openURL(NSURL(string:phoneNumber)!)
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4929717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档