I am using a collection view and collection view reusable header. In the header, i am trying to get the user's location. I keep getting this error:
Trying to start MapKit location updates without prompting for
location authorization. Must call -[CLLocationManager
requestWhenInUseAuthorization] or -[CLLocationManager
requestAlwaysAuthorization] first.
I looked at this question: Location Services not working in iOS 8 and none of the proposed solutions worked for me. I have added the keys (both NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription) to my info.Plist and that did not help. The app never requests the user to get it's location. Here is my code:
EDIT: my code now:
import UIKit
import MapKit
import Parse
import ParseUI
import CoreLocation
class ReusableHeaderMap: UICollectionReusableView, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
@IBOutlet weak var newMap: MKMapView!
override func awakeFromNib() {
locationManager.delegate = self
let status = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.Denied {
}
else if status == CLAuthorizationStatus.NotDetermined {
self.locationManager.requestAlwaysAuthorization()
}
else if status == CLAuthorizationStatus.AuthorizedAlways {
self.locationManager.startUpdatingLocation()
}
else {
print("it took the else route")
}
print("status is \(status)")
self.newMap.layer.cornerRadius = 8
self.newMap.clipsToBounds = true
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locations.count > 0 {
locationManager.stopUpdatingLocation()
let location1 = locations.first as CLLocation!
let mapSpan = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let mapRegion = MKCoordinateRegion(center: location1.coordinate, span: mapSpan)
self.newMap.setRegion(mapRegion, animated: true)
}
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.Denied {
print("there will be limited functionaliy")
}
else if status == CLAuthorizationStatus.AuthorizedAlways {
self.locationManager.startUpdatingLocation()
}
else {
print("it took the else route")
}
}
}
my info.plist has this key:
<key>NSLocationAlwaysUsageDescription</key>
<string>Get User Location</string>
CLLocationManager.authorizationStatus()
? And is yourdidChangeAuthorizationStatus()
method being called? – Duncan C