user3745888
user3745888

Reputation: 6353

Not ask permission to access to coreLocation and crash in swift

I have CoreLocation in my Swift app. When I run this in simulator or in a device, this crash and not show the permissions to access CoreLocation.. I have all code necessary to implement this: request in code, NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in plist... I'm reading iOS app doesn't ask for location permission but I can't make that this show.

 var location: CLLocationManager!
let status = CLLocationManager.authorizationStatus()
  location=CLLocationManager()
  if(status == CLAuthorizationStatus.NotDetermined) {
        self.location.requestAlwaysAuthorization();
    }
    else {
        location.startUpdatingLocation()
    }
    location.delegate = self
    location.desiredAccuracy=kCLLocationAccuracyBest 
    self.location.startMonitoringSignificantLocationChanges()

    print(location.location.coordinate.latitude) //Here crash
    print(location.location.coordinate.longitude)

What other things can I make for show this?

Thanks!

Upvotes: 1

Views: 1471

Answers (3)

MacKa
MacKa

Reputation: 59

You have to add a row to info.plist file

NSLocationWhenInUseUsageDescription which contains a string.

That string will be displayed when the alert pops up asking user's permission.

Upvotes: 0

MirekE
MirekE

Reputation: 11555

The prompt only shows if the status is .NotDetermined. There are other statuses though that would prevent the location services from working (all but .Authorized) when the prompt does not get displayed. You should check for these.

Also, the requestAlwaysAuthorization does not return immediately and you need to implement

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) 

Upvotes: 0

zisoft
zisoft

Reputation: 23078

Here is a code snippet from my app:

    let locationManager = CLLocationManager()
    ...
    locationManager.delegate = self
    locationManager.activityType = CLActivityType.Fitness
    locationManager.distanceFilter = 10   // 10m

    if ( UIDevice.currentDevice().systemVersion == "8.0" ) {
        locationManager.requestAlwaysAuthorization()
    }

    // get current location
    if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
        locationManager.startUpdatingLocation()
    }

Upvotes: 0

Related Questions