How do I add the ability to make phone calls through a Right Callout Accessory View action button?

35 views Asked by At

What I'm looking to do is when someone presses a the "Call Location" button via the UIAlertAction sequence below, I want it to load up the phone and dial a phone number I imputed earlier in the code (up in ViewDidLoad). This is my annotationView - calloutAccessory code that I have to this point:

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)


}

How would I code this in?

Note: I'm new to coding, so any help is appreciated.

UPDATE: I figured this out on my own. In the first ac.addAction line, this is what I changed it to:

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)
}
})) 
0

There are 0 answers