iOS 8
如果需要在iOS中始终使用定位和在当使用应用的时候使用定位,需要在plist文件中添加

NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
这两个字段没什么特别的意思,就是自定义提示用户授权使用地理定位功能时的提示语。
iOS 9
设置plist文件

代码
在ViewDidLoad
中设置CLLocationManager
实例的属性
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
//在应用使用时,使用定位
locationManager.requestWhenInUseAuthorization()
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
} else {
//设置不是iOS 9.0 的情况
locationManager.requestAlwaysAuthorization()
}
//更新用户位置
locationManager.startUpdatingLocation()
然后实现代理CLLocationManagerDelegate
中的方法
网友评论