Checking location service permission on iOS

前端 未结 8 1829
有刺的猬
有刺的猬 2020-12-07 12:32

How can I check if location service is enabled for my app?

I have 2 storyboards and I want to check location service. If location service enabled for my app, I want

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 12:50


    Swift 3.0 & iOS 10 Solution:


    self.locationManager?.requestWhenInUseAuthorization()
    if CLLocationManager.locationServicesEnabled() && CLLocationManager.authorizationStatus() != CLAuthorizationStatus.denied {
                locationManager?.delegate = self
                locationManager?.desiredAccuracy = kCLLocationAccuracyBestForNavigation
                locationManager?.distanceFilter = distanceFiler
                locationManager?.startUpdatingLocation()
            }else{
                let alertView = UIAlertView(title: "Location Services Disabled!", message: "Please enable Location Based Services for better results! We promise to keep your location private", delegate: self, cancelButtonTitle: "Settings", otherButtonTitles: "Cancel")
                alertView.delegate = self
                alertView.show()
                return
            }
    
    
    @objc(alertView:clickedButtonAtIndex:) func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
        if buttonIndex == 0 {
                if let url = URL(string: "App-Prefs:root=LOCATION_SERVICES") {
                    UIApplication.shared.open(url, completionHandler: .none)
                }
        }
        else if buttonIndex == 1 {
            //TODO for cancel
        }
    
    }
    

提交回复
热议问题