Skip to content

Commit

Permalink
.Always authorization is required for significant location updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Tres Spicher committed Dec 4, 2015
1 parent 7e29801 commit 9cea668
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 8 additions & 6 deletions THGLocation/LocationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -284,22 +284,24 @@ 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()
}
}

/**
* 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? {
Expand Down

0 comments on commit 9cea668

Please sign in to comment.