Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocationManager requestWhenInUseAuthorization() not working

I am trying to use location services in my iOS app but for some reason requestWhenInUseAuthorization is not working. When the user first uses the app, the prompt comes up as normal asking for permissions but then when you open the app a second time, for some reason didChangeAuthorizationStatus method is not called so I cannot display the user current location on the map.

My code is below:

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    var config:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
    config.URLCache = NSURLCache(memoryCapacity: 2 * 1024 * 1024, diskCapacity: 10 * 1024 * 1024, diskPath: "MarkerData")
    markerSession = NSURLSession(configuration: config)
 }



 func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .AuthorizedWhenInUse {

        locationManager.startUpdatingLocation()
        mapView.delegate = self
        mapView.myLocationEnabled = true
        mapView.settings.myLocationButton = true
     }
 }
like image 346
user3536057 Avatar asked Dec 12 '22 00:12

user3536057


1 Answers

First you need to add NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription(if you want to use in background) in your info.plist file. See the following image:

enter image description here

Next, in your swift file, you need to call either locationManager.requestWhenInUseAuthorization() or locationManager.requestAlwaysAuthorization() in your viewDidLoad() method.

Finally, you can do a mapView.camera = GMSCameraPosition(target: locations.last!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0) in your locationManager delegate method.

Sample code:

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager = CLLocationManager();

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
            longitude: 151.20, zoom: 6)
        var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true
        self.view = mapView

        locationManager.delegate = self
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        if #available(iOS 8.0, *) {
            print("iOS >= 8.0.0")
            locationManager.requestAlwaysAuthorization()
        }
        locationManager.startUpdatingLocation()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        println(locations.last)

        var mapView = self.view as! GMSMapView

        mapView.camera = GMSCameraPosition(target: locations.last!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
    }
}

You can will this post, for more details about LocationManager change in iOS 8.

like image 159
ztan Avatar answered Mar 01 '23 23:03

ztan