Skip to content

Instantly share code, notes, and snippets.

@vatsal1992
Forked from igroomgrim/LocationService.swift
Last active April 15, 2019 02:32
Show Gist options
  • Save vatsal1992/6a6dc677e8a192225cd09ca6b67c75b9 to your computer and use it in GitHub Desktop.
Save vatsal1992/6a6dc677e8a192225cd09ca6b67c75b9 to your computer and use it in GitHub Desktop.
Simply Singleton CLLocationManager Class in Swift
//
// LocationService.swift
//
//
// Created by Anak Mirasing on 5/18/2558 BE.
//
//
import Foundation
import CoreLocation
protocol LocationServiceDelegate {
func tracingLocation(currentLocation: CLLocation)
func tracingLocationDidFailWithError(error: NSError)
}
class LocationService: NSObject, CLLocationManagerDelegate {
// class var sharedInstance: LocationService {
// struct Static {
// static var onceToken: dispatch_once_t = 0
// static var instance: LocationService? = nil
// }
// dispatch_once(&Static.onceToken) {
// Static.instance = LocationService()
// }
// return Static.instance!
// }
static let sharedInstance = LocationService() //Simple, Short & Sweet :)
var locationManager: CLLocationManager?
var lastLocation: CLLocation?
var delegate: LocationServiceDelegate?
override init() {
super.init()
self.locationManager = CLLocationManager()
guard let locationManager = self.locationManager else {
return
}
if CLLocationManager.authorizationStatus() == .NotDetermined {
// you have 2 choice
// 1. requestAlwaysAuthorization
// 2. requestWhenInUseAuthorization
locationManager.requestAlwaysAuthorization()
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest // The accuracy of the location data
locationManager.distanceFilter = 200 // The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
locationManager.delegate = self
}
func startUpdatingLocation() {
print("Starting Location Updates")
self.locationManager?.startUpdatingLocation()
}
func stopUpdatingLocation() {
print("Stop Location Updates")
self.locationManager?.stopUpdatingLocation()
}
// CLLocationManagerDelegate
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else {
return
}
// singleton for get last location
self.lastLocation = location
// use for real time update location
updateLocation(location)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
// do on error
updateLocationDidFailWithError(error)
}
// Private function
private func updateLocation(currentLocation: CLLocation){
guard let delegate = self.delegate else {
return
}
delegate.tracingLocation(currentLocation)
}
private func updateLocationDidFailWithError(error: NSError) {
guard let delegate = self.delegate else {
return
}
delegate.tracingLocationDidFailWithError(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment