为什么80%的码农都做不了架构师?>>>
最近在ios8.0使用CLLocationManager定位服务,发现老不能定位,查看设置菜单中的项也是处于未知状态.想起之前都有一个弹出框提示用户是否允许定位,这次一直没有出现了.原来ios8.0下的定位服务需要申请授权了. 具体代码如下:
首先在 info.plist里加入对应的缺省字段 ,值设置为YES(前台定位写上边字段,前后台定位写下边字段)
NSLocationWhenInUseUsageDescription //允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription //允许在前、后台获取GPS的描述
([[[UIDevice currentDevice] systemVersion] doubleValue] > 8.0)
{
//设置定位权限 仅ios8有意义
[
self
.locationManager requestWhenInUseAuthorization];
// 前台定位
// [locationManager requestAlwaysAuthorization];// 前后台同时定位
}
[
self
.locationManager startUpdatingLocation];
}
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest; //控制定位精度,越高耗电量越大。
_locationManager.distanceFilter = 100; //控制定位服务更新频率。单位是“米”
[_locationManager startUpdatingLocation];
//在iOS 8.0下要授权
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[_locationManager requestWhenInUseAuthorization]; //调用了这句,就会弹出允许框了.
}
注意:
在Info.plist文件还要加上NSLocationWhenInUseUsageDescription这个key,Value可以为空,
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation * currLocation = [locations lastObject];
NSLog(@"%@",[NSString stringWithFormat:@"%.3f",dinate.latitude]);
NSLog(@"%@",[NSString stringWithFormat:@"%.3f",dinate.longitude]);
}
转载于:
本文发布于:2024-01-29 17:30:20,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170652062417082.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |