Search code examples
iosswiftlocation

iOS Swift How to open Location Permission popup


var locMgr = INTULocationManager.sharedInstance()
    locMgr.requestLocation(withDesiredAccuracy: .city, timeout: 30, delayUntilAuthorized: true,block: {(currentLoc: CLLocation!, achievedAccuracy: INTULocationAccuracy, status: INTULocationStatus) -> Void in
        if status == INTULocationStatus.success {
        }
        else{
        }

Used INTULocationManager , Swift 4.1 , iOS 11.1

if first time run this code pop up Location Permission Request

but If I denied, this is not pop up next time.

how to open Permission Pop up?

i create Button

run this code

let locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()

but not worked


Solution

  • There isn't any default functionality which will popup the location permission once the user has denied the permission. You need to show an alert to the user that permission is required, and then redirect the user to Settings screen. Here's the complete code you can use. Define a function which will check the location permission.

        func hasLocationPermission() -> Bool {
            var hasPermission = false
            if CLLocationManager.locationServicesEnabled() {
                switch CLLocationManager.authorizationStatus() {
                case .notDetermined, .restricted, .denied:
                    hasPermission = false
                case .authorizedAlways, .authorizedWhenInUse:
                    hasPermission = true
                }
            } else {
                hasPermission = false
            }
            
            return hasPermission
        }
    

    Now check location permission through this function and show alert if needed.

        if !hasLocationPermission() {
                let alertController = UIAlertController(title: "Location Permission Required", message: "Please enable location permissions in settings.", preferredStyle: UIAlertControllerStyle.alert)
                
                let okAction = UIAlertAction(title: "Settings", style: .default, handler: {(cAlertAction) in
                    //Redirect to Settings app
                    UIApplication.shared.open(URL(string:UIApplicationOpenSettingsURLString)!)
                })
                
                let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel)
                alertController.addAction(cancelAction)
                
                alertController.addAction(okAction)
                
                self.present(alertController, animated: true, completion: nil)
            }
    

    Also don't forget to import CoreLocation.

    Swift 5.3 and iOS 14 version

    func hasLocationPermission() -> Bool {
        var hasPermission = false
        let manager = CLLocationManager()
        
        if CLLocationManager.locationServicesEnabled() {
            switch manager.authorizationStatus {
            case .notDetermined, .restricted, .denied:
                hasPermission = false
            case .authorizedAlways, .authorizedWhenInUse:
                hasPermission = true
            @unknown default:
                    break
            }
        } else {
            hasPermission = false
        }
        
        return hasPermission
    }
    

    if !hasLocationPermission() {
        let alertController = UIAlertController(title: "Location Permission Required", message: "Please enable location permissions in settings.", preferredStyle: .alert)
        
        let okAction = UIAlertAction(title: "Settings", style: .default, handler: {(cAlertAction) in
            //Redirect to Settings app
            UIApplication.shared.open(URL(string:UIApplication.openSettingsURLString)!)
        })
        
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        alertController.addAction(cancelAction)
        
        alertController.addAction(okAction)
        
        self.present(alertController, animated: true, completion: nil)
    }