tudoricc
tudoricc

Reputation: 729

swift user's location issue when trying to get current location

I am doing an app wich uses the current user location and of it's friend's agenda. I wrote a little bit of code which is suppossed to show your location

    var loc :CLLocationCoordinate2D = self.mapView.userLocation.coordinate
    var region = MKCoordinateRegionMakeWithDistance(loc, 5, 5)
    self.mapView.setRegion(region, animated: true)

Furthermore I am getting the error saying :

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

After some research I don't understand what I'm supposed to do to improve my location[i changed from debug->simulateLocation but nothing changed]

Can you please explain what I have to keep in mind and what I need to do to improve my app?

[edit1] Is it possible that the simulator behaves this way and the phone should not behave the same way?

Upvotes: 1

Views: 1993

Answers (2)

zisoft
zisoft

Reputation: 23078

As of iOS 8 you have to explicitly ask the user for permission:

    var systemVersion = NSOperatingSystemVersion(majorVersion: 8, minorVersion: 0, patchVersion: 0)
        if NSProcessInfo.processInfo().isOperatingSystemAtLeastVersion(systemVersion) {
        locationManager.requestAlwaysAuthorization()
    }

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

Furthermore you have to explicitly declare the permission question in your Info.plist

NSLocationAlwaysUsageDescription

Upvotes: 1

tudoricc
tudoricc

Reputation: 729

I figured this issue out by google-ing it a little bit:

http://rshankar.com/get-your-current-address-in-swift/

One of the steps is going to settings->General->...Use Location->Always,This is suppossed to solve it and you will not get that error.

Furthermore the person that made the tutorial mentioned that you should enter a location just ot be sure it works and VOILA!

Upvotes: 0

Related Questions