Reputation: 71
I have a problem with showing location on physical device when locationManager is set to startMonitoringSignificantLocationChanges()
.
Everything works in Simulator but when I make it run on my iPhone there is no coordinates returned, and if I change it to startUpdatingLocation()
it works on my physical device.
Any idea what could be wrong?
My code:
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
startReceivingSingificantLocationChanges()
}
func startReceivingSingificantLocationChanges() {
if !CLLocationManager.significantLocationChangeMonitoringAvailable() {
//the service is not available
print("Service is not available")
return
}
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startMonitoringSignificantLocationChanges()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first{
print(location.coordinate)
coordinatesLbl.text = "LAT:\(location.coordinate.latitude) LONG:\(location.coordinate.longitude)"
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.denied{
showLocationDisabledPopUp()
print("didChangeAuthorization")
}
}
Upvotes: 1
Views: 656
Reputation: 1475
I have tried this and had the same issue. I ended up setting the location desiredAccuracy
to kCLLocationAccuracyKilometer
. This had the desired effect of giving me significant location changes. Another way to do this is to use startUpdatingLocation
and once you have the current location, change the location manager to startMonitoringSignificantLocationChanges
.
Upvotes: 1
Reputation: 19758
The documentation on startMonitoringSignificantLocationChanges()
states:
Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.
So you shouldn't expect them very often when using this method.
startUpdatingLocation
updates much more often, which is why this works.
Upvotes: 1