CLLocation Manager in Swift to get Location of User

后端 未结 13 2075
不知归路
不知归路 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:24

    Just call the init(vc : UIViewController).

        import Foundation
        import CoreLocation
        import UIKit
    
    
        class LocManager : NSObject{
    
    
            var permission : ((Bool?)->())?
    
            private var locationManager : CLLocationManager!
    
            init(_ vc : UIViewController) {
                super.init()
                self.locationManager = CLLocationManager()
                self.locationManager.delegate = vc as? CLLocationManagerDelegate
                setUpLocationManagerDelegate()
            }
    
    
        }
    
        extension LocManager : CLLocationManagerDelegate {
    
            fileprivate func setUpLocationManagerDelegate(){
                   locationManager = CLLocationManager()
                   locationManager.delegate = self
                   locationManager.desiredAccuracy = kCLLocationAccuracyBest
                   locationManager.requestAlwaysAuthorization()
               }
    
            func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
                if let lat  = locations.last?.coordinate.latitude, let long = locations.last?.coordinate.longitude{
                    print("\n\nThe current Lat/Long Is Here\n\n")
                    let coordinates = CLLocationCoordinate2D(latitude: lat, longitude: long)
    
                }else{
                    print("Unable To Access Locaion")
                }
            }
    
            func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    
                switch status {
                case .authorizedAlways,.authorizedWhenInUse:
                    print("Good to go and use location")
                    locationManager.startUpdatingLocation()
                    self.callPermisssionCompletion(val: true)
    
                case .denied:
                    print("DENIED to go and use location")
                    self.callPermisssionCompletion(val: false)
    
                case .restricted:
                    print("DENIED to go and use location")
                    self.callPermisssionCompletion(val: nil)
    
                case .notDetermined:
                    print("DENIED to go and use location")
                    self.callPermisssionCompletion(val: nil)
    
                default:
                    print("Unable to read location :\(status)")
                }
            }
    
    
            fileprivate func callPermisssionCompletion(val : Bool?){
    
                guard let comp = self.permission else {
                    print("\n\n Unable to  locate completions \n\n")
                    return
                }
                if let val =  val{
                    comp(val)
                }
    
            }
    
    
        }
    

提交回复
热议问题