Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> iOS

Thực hiện cuộc gọi điện thoại trong iOS 10 bằng Swift

Dùng UIApplication.shared.open(_:options:completionHandler:) với scheme tel://.

  1. Storyboard: TextField nhập số, Button gọi.
  2. @IBOutlet textField, @IBAction button.
  3. Trong action:
@IBAction func callButtonClicked(_ sender: UIButton) {
  guard let phone = phoneNumberTextfield.text, !phone.isEmpty,
        let url = URL(string: "tel://\(phone)"),
        UIApplication.shared.canOpenURL(url) else { return }
  UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

Hệ thống hiện confirm "Call / Cancel", người dùng chọn Call thì thực hiện cuộc gọi.