Skip to content

Instantly share code, notes, and snippets.

@Juanpe
Last active December 9, 2018 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Juanpe/bc99fee4dc43478c7ae1e64979f3bbe1 to your computer and use it in GitHub Desktop.
Save Juanpe/bc99fee4dc43478c7ae1e64979f3bbe1 to your computer and use it in GitHub Desktop.
class UserLocationService: NSObject, UserLocationProvider {
fileprivate var provider: LocationProvider
fileprivate var locationCompletionBlock: UserLocationCompletionBlock?
init(with provider: LocationProvider) {
self.provider = provider
super.init()
}
func findUserLocation(then: @escaping UserLocationCompletionBlock) {
self.locationCompletionBlock = then
if provider.isUserAuthorized {
provider.requestLocation()
} else {
provider.requestWhenInUseAuthorization()
}
}
}
extension UserLocationService: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
provider.requestLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.stopUpdatingLocation()
if let location = locations.last {
locationCompletionBlock?(location, nil)
} else {
locationCompletionBlock?(nil, .canNotBeLocated)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment