iOS8下,CoreLocation的变化
2014-11-10 15:47
204 查看
打开一个老工程,发现里面location服务用不了了,CLLocationManager的代理方法根本就不执行了,一通查找,结论如下:
第一步:在iOS8里,使用location服务,需要在Info.plist文件里添加key:
NSLocationWhenInUseUsageDescription(在使用app时,即:foreground,允许)
或者
NSLocationAlwaysUsageDescription(总是允许,包括在app退到后台),
这两个key至少添加一个,value可以为空,也可以填写自己的描述,通常都是一些,“xxx需要使用大爷您的位置,求允许”之类的。
第二步:在初始化CLLocationManager对象时,需要调用
requestWhenInUseAuthorization
或者
requestAlwaysAuthorization
设置好这两步,回调的代理方法里就能得到位置信息了。
一个完整的例子:
第一步:在iOS8里,使用location服务,需要在Info.plist文件里添加key:
NSLocationWhenInUseUsageDescription(在使用app时,即:foreground,允许)
或者
NSLocationAlwaysUsageDescription(总是允许,包括在app退到后台),
这两个key至少添加一个,value可以为空,也可以填写自己的描述,通常都是一些,“xxx需要使用大爷您的位置,求允许”之类的。
第二步:在初始化CLLocationManager对象时,需要调用
requestWhenInUseAuthorization
或者
requestAlwaysAuthorization
设置好这两步,回调的代理方法里就能得到位置信息了。
一个完整的例子:
#import "ViewController.h" @import CoreLocation; @interface ViewController () <CLLocationManagerDelegate> @property (strong, nonatomic) CLLocationManager *locationManager; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // ** Don't forget to add NSLocationWhenInUseUsageDescription in MyApp-Info.plist and give it a string self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7. if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [self.locationManager requestWhenInUseAuthorization]; } [self.locationManager startUpdatingLocation]; } // Location Manager Delegate Methods - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { NSLog(@"%@", [locations lastObject]); } @end
相关文章推荐
- ios8 - CoreLocation定位服务的变化
- iOS8 - CoreLocation的变化
- 迎接iOS8 - CoreLocation的变化
- 迎接iOS8 - CoreLocation的变化
- iOS8 Core Location (授权。。。室内定位。。。访问监控。。。)
- iOS8之后CoreLocation定位的使用
- Core Location Manager Changes in iOS 8(iOS8获取不到地理位置)
- iOS CoreLocation框架第一章—— CLLocationManager的使用(包含iOS6,iOS8,iOS9各种变动)
- Core Location Manager在iOS8中的改变
- CoreLocation Authorization in iOS8+
- iOS CoreLocation使用(iOS8 回调不行的问题解决)
- Core Location Manager在IOS8中的改变
- iOS8以后的屏幕宽高变化
- Xcode6下iOS8地图无法定位问题Trying to start MapKit location updates without prompting for location authorizat
- iOS8中定位服务的变化 CLLocationManager协议方法不响应
- iOS学习笔记 地图(一)定位CoreLocation
- shows user location在iOS8上不能使用地图定位
- CoreLocation基本使用
- iPhone开发之CoreLocation定位功能(6)
- ios8中用swift实现文本输入框适应键盘高度变化