Many apps requires user's location to perform the required features or enhance the user experiences. Every year of WWDC provides better funtionailty for developers to access and provides more control for the users to handle app-wise locational permissions.
allowsBackgroundLocationUpdates property per CLLocationManager-responsdToSelector:requestLocation by giving desiredAccuracy to locationManager .locationManager:didUpdateLocations: or locationManager:didFailWithError// from : http://nshipster.com/ios9/
class ViewController : UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
// ...
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.requestLocation()
}
// MARK: - CLLocationManagerDelegate
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
print("Current location: \(location)")
} else {
// ...
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error finding location: \(error.localizedDescription)")
}
}
requestWhenInUserAuthorizationrequestAlwaysAutherization| iOS | WhenInUse | Always |
|---|---|---|
| Location | YES | YES |
| Background(continuous) | YES | YES |
| Background(intermittent) | NO | YES |
| Monitor | Local notifiation | YES |
| watchOS | WhenInUse | Always |
|---|---|---|
| Location | Single | Single |
| Background(continuous) | NO | NO |
| Background(intermittent) | NO | Single |
| Monitor | NO | NO |
Accuracy with iPhone
requestLocation Even indoorAccuracy without iPhone
requestLocation kCLLocationAccurayHundredMeterssendMessage:replyHandler:errorHanlder: (watch to phone)allowsBackgroundLocationUpdates yes to get location updateApplicationContent: (iPhone to Apple Watch)-allowDeferredLocationUpdateUntilTraveled:timeout: use this latency to update location based on distance traveled or time passed.