Last active
May 25, 2021 03:31
-
-
Save malcommac/9abd795779a43a0d98ca704d1c4b2778 to your computer and use it in GitHub Desktop.
CLLocationManager Dummy Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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