您的位置:首页 > 移动开发 > IOS开发

ios 8使用定位

2015-06-12 12:23 337 查看
#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager* locationManager;

@property (strong, nonatomic) UILabel *city;

@end

@implementation ViewController

@synthesize locationManager,city;

- (void)viewDidLoad {

[super viewDidLoad];

UIButton *getLocationBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

[getLocationBtn setTitle:@"获取位置" forState:UIControlStateNormal];

[getLocationBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

getLocationBtn.layer.borderWidth = 0.5f;

[getLocationBtn addTarget:self action:@selector(startLocation) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:getLocationBtn];

city = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, 50)];

[city setTextAlignment:NSTextAlignmentCenter];

[self.view addSubview:city];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

//开始定位

-(void)startLocation{

self.locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

self.locationManager.distanceFilter = 10.0f;

[self.locationManager requestAlwaysAuthorization];

// [CLLocationManager authorizationStatus];

[self.locationManager startUpdatingLocation];

}

//定位代理经纬度回调

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

[locationManager stopUpdatingLocation];

NSLog(@"location ok");

NSLog(@"%@",[NSString stringWithFormat:@"经度:%3.5f\n纬度:%3.5f",newLocation.coordinate.latitude,newLocation.coordinate.longitude]);

CLGeocoder * geoCoder = [[CLGeocoder alloc] init];

[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {

for (CLPlacemark * placemark in placemarks) {

NSDictionary *test = [placemark addressDictionary];

// Country(国家) State(城市) SubLocality(区)

NSLog(@"%@", [test objectForKey:@"State"]);

[city setText:[test objectForKey:@"Name"]];

}

}];

}

@end

我的一个程序,以前都好用的,包括ios6,ios7,但是手机升级到iOS8后,xcode6编译运行,就不能正常工作了,也不报错。现象是进到设置里隐私-定位服务下面找到这个应用,设为总是允许,就可以正常获取地理信息,但是再重新编译运行,它又会变成这个未知状态。

琢磨后想起来以前重装应用再调用定位服务时会弹出授权对话框,但是IOS8怎么没弹呢,于是加入调用locationManager requestAlwaysAuthorization手动申请授权,然后你可以在didChangeAuthorizationStatus这个回调里面监测是否可以具有定位权限。但是有几点要注意当你调用这个request的时候,它弹出的对话框里面的那句询问语,需要我们自己指定。所以需要在info.plist里面添加一个key: NSLocationAlwaysUsageDescription,value就是对话框上想要询问的语句。这时候程序才能正确的弹出授权对话框,用户选了同意以后,定位代码就正确的运行。

_instance.locationManager = [[CLLocationManager alloc] init];//创建位置管理器	_instance.locationManager.delegate=_instance;	_instance.locationManager.desiredAccuracy=kCLLocationAccuracyBest;	_instance.locationManager.distanceFilter=100.0f;	_instance.updating=NO;	//定位服务是否可用	BOOL enable=[CLLocationManager locationServicesEnabled];	//是否具有定位权限	int status=[CLLocationManager authorizationStatus];	if(!enable || status<3){	//请求权限	[_instance.locationManager requestWhenInUseAuthorization];	}

在看看authorizationStatus的枚举吧

typedef NS_ENUM(int, CLAuthorizationStatus) {	// 用户从未选择过权限	kCLAuthorizationStatusNotDetermined = 0,	// 无法使用定位服务,该状态用户无法改变	kCLAuthorizationStatusRestricted,	// 用户拒绝该应用使用定位服务,或是定位服务总开关处于关闭状态	kCLAuthorizationStatusDenied,	// 这个值已弃用	kCLAuthorizationStatusAuthorized // 大致是用户允许该程序无论何时都可以使用地理信息	kCLAuthorizationStatusAuthorizedAlways // 大致是用户同意程序在可见时使用地理位置	kCLAuthorizationStatusAuthorizedWhenInUse };

也就是[CLLocationManager authorizationStatus]返回大于等于3时才是可以使用定位服务的。

总结一下这个问题就是在以前的IOS版本中当开始使用定位服务时会自动弹出询问授权的对话框,而现在IOS8需要手动调用locationManager requestAlwaysAuthorization手动申请授权,来获取定位权限。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios