您需要在函数外添加函数,并按如下方式调用。
func openWaze(location : CLLocationCoordinate2D) {
if UIApplication.shared.canOpenURL(URL(string: "waze://")!) {
// Waze is installed. Launch Waze and start navigation
let urlStr: String = "waze://?ll=\(location.latitude),\(location.longitude)&navigate=yes"
UIApplication.shared.openURL(URL(string: urlStr)!)
}
else {
// Waze is not installed. Launch AppStore to install Waze app
UIApplication.shared.openURL(URL(string: "http://itunes.apple.com/us/app/id323229106")!)
}
}
呼叫方式如下
let WazeButton = UIAlertAction(title: "Waze", style: .default) { action -> Void in
self. openWaze(location : your corrdinates)// call here your function
print("Waze Chosen!")
}