CLLocation Manager in Swift to get Location of User

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

    You are missing two things. First, you have to ask for permission using requestAlwaysAuthorization or requestWhenInUseAuthorization(). So your viewDidLoad() should be like this:

    var locationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }
    

    Second, edit your Info.plist as indicated here.

提交回复
热议问题