UIAlertController按钮功能不起作用

时间:2017-07-09 18:03:28

标签: ios swift function uialertcontroller

let location =  CLLocationCoordinate2D(latitude: 32.075300, longitude: 34.782563)

    @IBAction func DirectionsTolocationButton(_ sender: Any) {
        // Create the AlertController and add its actions like button in ActionSheet
        let ActionSheet = UIAlertController(title: "Please Select A Navigation Service.", message: nil, preferredStyle: .actionSheet)

        let AppleMapsButton = UIAlertAction(title: "Apple Maps", style: .default) { action -> Void in

            let destinationName = (self.barNameTemplate ) 
            self.openMapsAppWithDirections(to: self.CoordinatesTemplate, destinationName: destinationName)
            print("Apple Map Chosen!")

        }
        let WazeButton = UIAlertAction(title: "Waze", style: .default) { action -> Void in

            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")!)
                }
            }
            print("Waze Chosen!")
        }

当我选择WazeButton时,没有任何事情发生......正如Waze在他们的API中所说的那样,我将此添加到我的Plist中:

<key>LSApplicationQueriesSchemes</key>
<array>
        <string>waze</string>
</array>

为什么它不适合我?

1 个答案:

答案 0 :(得分:5)

您需要在功能外添加功能,并按以下方式调用。

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!")
        }