I want to upload a user's location to a server to send them a location-sensitive alert, so the location needs to be fairly accurate:
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 #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
} else {
// Fallback on earlier versions
};
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 = 2000 // 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
}
// Check if Application is active
// Check Current View Controller
// Refresh Weather
// 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){
let todaysDate:NSDate = NSDate()
let dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
let DateInFormat:String = dateFormatter.stringFromDate(todaysDate)
print(DateInFormat)
print(currentLocation.coordinate.latitude.description + ", " + currentLocation.coordinate.longitude.description)
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)
}
}
This code does work, although I'm not sure how well the location accuracy and update works. Please review this code.