MapView does not ask for placement permission

I am trying to build a mapView in Swift. This already worked, but after I changed something I can't remember, now I get the following error:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

      

NSLocationAlwaysUsageDescription

is in my .plist file.

Here is the code:

import UIKit
import MapKit

class AwesomeMap : UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    var map: MKMapView?
    var manager: CLLocationManager?

    func setup() {

        manager = CLLocationManager()
        manager!.delegate = self

        map!.delegate = self // map is being set from another controller

        manager!.requestAlwaysAuthorization()
        manager!.startUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        println("permisison did change")
        if(status == CLAuthorizationStatus.AuthorizedWhenInUse || status == CLAuthorizationStatus.Authorized) {
          map!.showsUserLocation = true
        }
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        for location in (locations as Array) {
            var loc = (location as CLLocation)
            println(loc.coordinate.latitude)

            let region = MKCoordinateRegion(center: loc.coordinate, span: MKCoordinateSpanMake(0.05, 0.05))
            map!.setRegion(region, animated: true)
        }
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        println("fail")
    }

}

      

Old code before doing a few sentences:

import UIKit
import MapKit

class AwesomeMap : UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var manager = CLLocationManager()
var map: MKMapView?

func setup(mapView: MKMapView) { // passing the IBOutlet from another controller

    // the change I made was around the following six lines I think...

    map = mapView
    map!.delegate = self

    manager.delegate = self
    manager.requestAlwaysAuthorization()

    map!.showsUserLocation = true
    manager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    for location in (locations as Array) {
        var loc = (location as CLLocation)
        println(loc.coordinate.latitude)

        let region = MKCoordinateRegion(center: loc.coordinate, span: MKCoordinateSpanMake(0.05, 0.05))
        map!.setRegion(region, animated: true)
    }
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("fail") // nothing happens here
}

      

}

+3


source to share


4 answers


You are allowed to call

map!.showsUserLocation = true

      

after your location manager has permission to use the user's location.



As in (there is no quick version of this, but you will probably get the point)

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
     if(status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways)
    {
        self.mapView.showsUserLocation = YES;
    }
}

      

+2


source


I have this working code in my application (shorthand):

if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
    manager.startUpdatingLocation()
}

      



Additionally, you can react to any changes in the user's authorization status:

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

      

0


source


Do not call showUserLocation when authorization status is undefined.

func setup(mapView: MKMapView) { // passing the IBOutlet from another controller

    map = mapView
    map!.delegate = self

    manager.delegate = self
    manager.requestAlwaysAuthorization() // Does not if status is determined.

    self.locationManager(manager: manager, status: CLLocationManager.authorizationStatus())
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .Authorized {
        map!.showsUserLocation = true
        manager.startUpdatingLocation()
    }
}

      

0


source


Go to simulator settings

Go to Simulator settings

after that enter search services and enable it. if you need the location of the blue object in the MapView enter <> and set <When using the app β†’

after that enter Location Services and turn on.  if you need location blue dote in MapView enter <<YourAppName>> and set << While Using the App >>

0


source







All Articles