在iOS6之前,对于地图的定位使用CLLocationManager来创建位置管理器,而今年当苹果官方升级之后,这就需要开发者们自己来打开位置管理,然后使用代码来实现.
更新之后,如果我们不进行添加缺省字符串,那么Xcode将会报错:Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
报错的同时,模拟器或手机都不会显示"是否允许打开定位"的提示框!这时候我们该怎么做呢?!!!
在Xcode中找到工程的plist文件,然后在plist文件中添加"NSLocationWhenInUseUsageDescription"缺省字符,其他的设置如图所示,然后在初始化位置管理器的时候,我们再添加以下代码
// 如果设备的当前系统时iOS8以上的系统,则自定义提示用户使用地理定位功能时的提示语
if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0) {
[_locationManager requestWhenInUseAuthorization];
}