I am facing the issues while fetching users location on Apple Watch. When I try to run on simulator I am not able to get the location but running on actual watch it returns me location. But there are some cases which don't return me location. Why does that happen?
Also, how should I do entries for location access in info.plist? Currently I have added to both iPhone and Apple Watch app.
This is how I request location.
func requestLocation() {
guard !isRequestingLocation else {
manager.stopUpdatingLocation()
isRequestingLocation = false
return
}
let authorizationStatus = CLLocationManager.authorizationStatus()
switch authorizationStatus {
case .notDetermined:
print("notDetermined")
isRequestingLocation = true
manager.requestWhenInUseAuthorization()
case .authorizedWhenInUse, .authorizedAlways:
print("authorizedWhenInUse")
isRequestingLocation = true
manager.requestLocation()
infoLabel.setText("Fetching Location...")
case .denied:
print("denied location")
case .restricted:
print("restricted location")
}
}
Below delegate method returns me location when found.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard !locations.isEmpty else { return }
DispatchQueue.main.async {
self.currentLocation = locations.last!.coordinate
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
DispatchQueue.main.async {
print(error.localizedDescription)
self.currentLocation = nil
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
DispatchQueue.main.async {
guard self.isRequestingLocation else { return }
switch status {
case .authorizedWhenInUse:
manager.requestLocation()
case .denied:
print("Auth Denied")
self.currentLocation = nil
default:
print("Auth Default")
self.currentLocation = nil
}
}
}