十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
在ios7之前,我们进入程序提示用户开启定位是这样做的,如下:
10年积累的网站建设、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有肇源免费网站建设让你可以放心的选择与我们合作。
CLLocationManager * locationManager = [[CLLocationManager alloc] init];//创建位置管理器
//locationManager.delegate=_instance;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=100.0f;
//定位服务是否可用
BOOL enable=[CLLocationManagerlocationServicesEnabled];
//是否具有定位权限
int status=[CLLocationManager authorizationStatus];
if(!enable || status<3){
//请求权限
[locationManager requestWhenInUseAuthorization];
}
但是到了ios8后,发现定位压根就不起作用,老是提示:
ios8定位解决如下:
先在info.plist定义key:(总是授权)NSLocationAlwaysUsageDescription或者使用时授权NSLocationWhenInUseUsageDescription
在.h里面
继承代码CLLocationManagerDelegate
定义CLLocationManager *_locationManager;
在.m里面定义:
- (void)startTrackingLocation {
CLAuthorizationStatus status = [CLLocationManagerauthorizationStatus];
if (status ==kCLAuthorizationStatusNotDetermined) {
//总是授权
[_locationManagerrequestAlwaysAuthorization];
//每次授权一次
//[_locationManager requestWhenInUseAuthorization];
}
elseif (status ==kCLAuthorizationStatusAuthorizedWhenInUse || status ==kCLAuthorizationStatusAuthorizedAlways) {
[_locationManagerstartUpdatingLocation];
}
}
//实现代码回调
#pragma mark - CLLocationManager Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
casekCLAuthorizationStatusAuthorizedAlways:
casekCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"Got authorization, start tracking location");
[selfstartTrackingLocation];
break;
casekCLAuthorizationStatusNotDetermined:
[_locationManagerrequestAlwaysAuthorization];
break;
default:
break;
}
}
调用如下:
if (IOS8) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate =self;
[selfstartTrackingLocation];
}
最后实现效果如下图,点击Allow即可定位了: