Skip to content

Instantly share code, notes, and snippets.

@igroomgrim
Last active September 10, 2023 18:06
Show Gist options
  • Save igroomgrim/ac1d46b5aea1173e6760 to your computer and use it in GitHub Desktop.
Save igroomgrim/ac1d46b5aea1173e6760 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!
}
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)
}
}
@zohaibmanzoorkushwaha
Copy link

how to use this class plz help me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment