I am not able to get current location in iPhone device. The code in ViewController is given below.
When I run the code, console shows NotDetermined and after about 5 to 7 seconds print the following error from didUpdateLocations
function:
got following error from loaction manager
Error Domain=kCLErrorDomain Code=0 "(null)"
I have following keys setup in Info.plist:
Required device apabilities
item 0 location-services
item 1 gps
Privacy - Location Usage Description Required to show your location
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
@IBOutlet weak var mapView: MKMapView!
func checkLocationAuthorizationStatus() {
let status = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.NotDetermined{
print("NotDetermined")
locationManager.requestWhenInUseAuthorization()
CLLocationManager.locationServicesEnabled()
locationManager.requestLocation()
}else {
print("Problem with authorization")
}
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
checkLocationAuthorizationStatus()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("got location update")
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("got following error from loaction manager")
print(error.description)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
func locationManager(manager: CLLocationManager,
didChangeAuthorizationStatus status: CLAuthorizationStatus){
print("didChangeAuthorizationStatus")
checkLocationAuthorizationStatus()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}