From 9cea668f16683c02ce9a1622a84bc1340e4bbce9 Mon Sep 17 00:00:00 2001 From: Tres Spicher Date: Thu, 3 Dec 2015 16:06:56 -0800 Subject: [PATCH] .Always authorization is required for significant location updates --- README.md | 2 +- THGLocation/LocationService.swift | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2d0b657..c9de12a 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ It is also possible to limit the callback frequency using the `updateFrequency` let request = LocationUpdateRequest(accuracy: .Good, updateFrequency: .ChangesOnly, ...) ``` -Combining `accuracy: .Coarse` with `updateFrequency: .ChangesOnly` yields the lowest battery usage, at the expense of less accurate location data. +Combining `accuracy: .Coarse` with `updateFrequency: .ChangesOnly`, along with `requestAuthorization(.Always)` yields the lowest battery usage, at the expense of less accurate location data and infrequent updates. ## Contributions diff --git a/THGLocation/LocationService.swift b/THGLocation/LocationService.swift index d7b32c4..801e5b1 100644 --- a/THGLocation/LocationService.swift +++ b/THGLocation/LocationService.swift @@ -46,7 +46,7 @@ More time and power is used going down this list as the system tries to provide so be conservative according to your needs. `Good` should work well for most cases. */ public enum LocationAccuracy: Int { - case Coarse = 0 + case Coarse case Good case Better case Best @@ -284,10 +284,10 @@ class LocationManager: NSObject, LocationUpdateProvider, LocationAuthorizationPr // MARK: Internal Interface private func startMonitoringLocation() { - if self.shouldUseSignificantUpdateService() { - self.manager.startMonitoringSignificantLocationChanges() + if shouldUseSignificantUpdateService() { + manager.startMonitoringSignificantLocationChanges() } else { - self.manager.startUpdatingLocation() + manager.startUpdatingLocation() } } @@ -295,11 +295,13 @@ class LocationManager: NSObject, LocationUpdateProvider, LocationAuthorizationPr * There are two kinds of location monitoring in iOS: significant updates and standard location monitoring. * Significant updates rely entirely on cell towers and therefore have low accuracy and low power consumption. * They also fire infrequently. If the user has requested location accuracy higher than .Coarse or wants - * continuous updates, the significant location service is inappropriate to use. Otherwise, it is a better option. + * continuous updates, the significant location service is inappropriate to use. Finally, the user must have + * requested .Always authorization status */ private func shouldUseSignificantUpdateService() -> Bool { let hasContinuousListeners = allLocationListeners.filter({$0.request.updateFrequency == .Continuous}).count > 0 - return !(hasContinuousListeners || accuracy.rawValue > LocationAccuracy.Coarse.rawValue) + let isAccuracyCoarseEnough = accuracy.rawValue <= LocationAccuracy.Coarse.rawValue + return !hasContinuousListeners && isAccuracyCoarseEnough && authorization == .Always } private func checkIfLocationServicesEnabled() -> NSError? {