我要做的是当有人按下“通话地点”时。通过下面的UIAlertAction序列按钮,我希望它能够加载电话并拨打我之前在代码中编入的电话号码(在ViewDidLoad中)。这是我的annotationView - calloutAccessory代码,我到目前为止:
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let stores = view.annotation as! Stores
let placeHours = stores.seasonhours
let placeHours1 = stores.offseasonhours
let phoneInfo = stores.phone
let ac = UIAlertController(title: placeHours, message: placeHours1, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Call Location", style: .default))
ac.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
present(ac, animated: true)
}
我将如何编写此代码?
注意:我是编码的新手,所以感谢任何帮助。
更新:我自己想出来了。在第一个ac.addAction行中,这是我将其更改为:
ac.addAction(UIAlertAction(title: "Call Location", style: .default, handler: {action in
if let url = URL(string: "tel://\(phoneInfo)") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}))