在 Swift Xcode 6 中,如何从 iPhone 应用程序呼叫?

9

我查看并尝试了之前关于如何直接从应用程序拨打电话的答案,但我不断收到错误提示。我只是在storryboard中创建了一个按钮,并将其连接到视图控制器。它一直给我报错“预期 ',' 分隔符”,但这并没有解决任何问题。我需要在AppDelegate或其他地方添加代码吗?我真的很想知道如何在Swift中实现这个功能。

import UIKit

class ViewController: UIViewController {

    @IBAction func theCallMeButtonMethod(sender: AnyObject) {
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt:0123456789"]]
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
       // Dispose of any resources that can be recreated.
    }


}

你的 Swift 代码里有一点 Objective-C 的痕迹。你现在的 Swift 代码是什么样子的? - jrturton
2个回答

40

Swift 3

这段代码将帮助你:

let phone = "tel://982374234"
let url = URL(string: phone)!
UIApplication.shared.open(url, options: [:], completionHandler: nil)

或者

let url = URL(string: "tel://9809088798")!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
   

1
谢谢,没有更多的错误了,但模拟器没有显示动作,这是因为它不能进行调用吗? - YannisDC
2
是的,模拟器无法进行电话呼叫。在真实设备上运行则没有问题。 - Rijo Payyappilly

8
这是如何在Swift中访问应用程序对象并打开URL的方法。
请替换下面的数字:
UIApplication.sharedApplication().openURL(NSURL(string:"telprompt:0123456789"))

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