如何使用openURL在Swift中拨打电话?

 email_osoo_240 发布于 2023-01-08 23:47

我已经将用于打电话的代码从Objective-C转换为Swift,但在Objective-C中,我们可以设置我们想要打开的URL类型(例如电话,短信,网络),如下所示:

@"tel:xx"
@"mailto:info@example.es"
@"http://stackoverflow.com"
@"sms:768number"

Swift中的代码是:

UIApplication.sharedApplication().openURL(NSURL(string : "9809088798")

我读过没有发布任何方案参数tel:,但我不知道Swift是否可以检测该字符串是用于打电话,发送电子邮件还是打开网站.或者我可以写:

(string : "tel//:9809088798")

5 个回答
  • 我很确定你想要:

    UIApplication.sharedApplication().openURL(NSURL(string: "tel://9809088798")!)
    

    (请注意,在你的问题文本中,你放了tel//:,而不是tel://).

    NSURL的字符串init需要格式良好的URL.它不会将一堆数字变成电话号码.您有时会在UIWebView中看到电话号码检测,但这比NSURL更高.

    编辑:添加!以下评论

    2023-01-08 23:51 回答
  • 对于iOS中的Swift:

    var url:NSURL? = NSURL(string: "tel://9809088798")
    UIApplication.sharedApplication().openURL(url!)
    

    2023-01-08 23:52 回答
  • 您需要记住删除空格,否则它将无法工作:

    if let telephoneURL = NSURL(string: "telprompt://\(phoneNumber.stringByReplacingOccurrencesOfString(" ", withString: ""))") {
            UIApplication.sharedApplication().openURL(telelphoneURL)
        }
    

    "telprompt://"将提示用户呼叫或取消,而"tel://"将直接呼叫.

    2023-01-08 23:53 回答
  • @ confile:

    问题是,在iOS7上完成电话呼叫后,您的解决方案不会返回应用程序. - 6月19日13:50

    &@ Zorayr

    嗯,好奇,如果有一个解决方案确实这样做..可能是对iOS的限制.

    使用

    UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://9809088798")!)
    

    您将收到呼叫/取消的提示,但它会返回到您的应用程序.AFAIK没有办法返回(没有提示)

    2023-01-08 23:56 回答
  • Swift中一个独立的解决方案:

    private func callNumber(phoneNumber:String) {
      if let phoneCallURL:NSURL = NSURL(string:"tel://\(phoneNumber)") {
        let application:UIApplication = UIApplication.sharedApplication()
        if (application.canOpenURL(phoneCallURL)) {
          application.openURL(phoneCallURL);
        }
      }
    }
    

    现在,您应该可以用来callNumber("7178881234")打电话了.

    2023-01-08 23:57 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有