UIAlertController button function not working

血红的双手。 提交于 2019-12-10 16:25:31

问题


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

When i choose WazeButton nothing happens... as Waze says in their API iv added this to my Plist as well :

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

why it doesn't work for me?


回答1:


You need to add function out side the function and call as below.

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

Calling as below

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

            self. openWaze(location : your corrdinates)// call here your function
            print("Waze Chosen!")
        }


来源:https://stackoverflow.com/questions/44999453/uialertcontroller-button-function-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!