Skip to content

Instantly share code, notes, and snippets.

@malcommac
Last active May 25, 2021 03:31
Show Gist options
  • Save malcommac/9abd795779a43a0d98ca704d1c4b2778 to your computer and use it in GitHub Desktop.
Save malcommac/9abd795779a43a0d98ca704d1c4b2778 to your computer and use it in GitHub Desktop.
CLLocationManager Dummy Example
import Foundation
import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
var manager:CLLocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestAlwaysAuthorization()
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .Restricted,.Denied,.NotDetermined:
// report error, do something
print("error")
default:
// location si allowed, start monitoring
manager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
manager.stopUpdatingLocation()
// do something with the error
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let locationObj = locations.last {
if locationObj.horizontalAccuracy < minAllowedAccuracy {
manager.stopUpdatingLocation()
// report location somewhere else
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment