一开始只写MKmapView初始化的时候,可以运行,但无法定位,出现如下提示:
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
首先我们要先在初始化MKmapView之前,初始化CLLocationManager,并且请求用户批准定位
locManager.delegate=self
locManager.requestAlwaysAuthorization()
locManager.startUpdatingLocation()
接下来要在plist文件中加入以下俩个Key
NSLocationWhenInUsageDescription // Value可以空也可以填,是自定义给用户的提示,类似 AppName is request your location
NSLocationAlwaysUsageDescription //同上
接下来再运行程序,成功定位
Happy coding : )