CLLocation Manager in Swift to get Location of User

后端 未结 13 1985
不知归路
不知归路 2020-11-27 04:48

I am trying to convert an old app in ObjC to Swift as a practice exercise and have ran in to some issues. The way I had it in the old app, it was establishing the CLLocatio

相关标签:
13条回答
  • 2020-11-27 05:35

    This will ask for permission and track if given permission else quit with an alert. Stops tracking on back button press.

    info.plist

    <key>NSLocationAlwaysUsageDescription</key>
    <string>Allow tracking while completing a survey</string>
    

    Class:

    import UIKit
    import CoreLocation    
    
    class LocationViewController: BaseViewController, CLLocationManagerDelegate {
    
            // MARK: Constants
    
            private static let enableLocationServices = [
                "title" : "Location",
                "message" : "Enable location services",
                "buttonTitle" : "OK"
            ]
    
            // MARK: Private variables
    
            private var manager: CLLocationManager?
    
            // MARK: UIViewCOntroller methods
    
            @IBAction func backButtonPressed(sender : UIButton) {
                stopTracking()
                detatchLocationManager()
                dismissViewControllerAnimated(true, completion: nil)
            }
    
            override func viewDidLoad() {
                super.viewDidLoad()
    
                attachLocationManager()    
            }
    
            // Mark: Location
    
            func locationManager(manager: CLLocationManager,
                                 didChangeAuthorizationStatus status: CLAuthorizationStatus)
            {
                if status == .AuthorizedAlways {
                    manager.startUpdatingLocation()
                } else if status != .NotDetermined {
                    showEnableLocationServicesAlert()
                }
            }
    
            func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
                for location in locations {
                    getDependencyService().getProject().appendLocationTrackingFile(location.timestamp, latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
                }
            }
    
            // MARK: LocationViewController
    
            private func attachLocationManager() {
                manager = CLLocationManager()
                manager?.delegate = self
                manager?.desiredAccuracy = kCLLocationAccuracyBest
    
                if CLLocationManager.authorizationStatus() != .AuthorizedAlways {
                    manager?.requestAlwaysAuthorization()
                } else if CLLocationManager.locationServicesEnabled() {
                    startTracking()
                }
            }
    
            private func detatchLocationManager() {
                manager?.stopUpdatingLocation()
                manager?.delegate = nil
                manager = nil
            }
    
            private func startTracking() {
                manager?.startUpdatingLocation()
            }
    
            private func stopTracking() {
                manager?.stopUpdatingLocation()
            }
    
            private func showEnableLocationServicesAlert() {
    getDependencyService().getUiHelper().showAlert(FrogFirstSurveyViewController.enableLocationServices, completion: {
                    self.dismissViewControllerAnimated(true, completion: nil)
                })
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题