iOS8 核心位置有问题
Having problems with iOS8 Core Location
Core Location 用法最近发生了变化,但即使更新到新规范后,我似乎也无法正常工作。
我尝试用一个视图控制器开始一个新项目,该视图控制器在 GPS 坐标查找或失败时更改其背景颜色。
#import "ViewController.h"
@interface ViewController (){
CLLocationManager *locationManager;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (locationManager == nil)
{
locationManager = [[CLLocationManager alloc] init];
}
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if (![CLLocationManager locationServicesEnabled]) {
[self.view setBackgroundColor:[UIColor redColor]];
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark CLLocationManagerDelegate
//
//
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
{
[self.view setBackgroundColor:[UIColor greenColor]];
NSLog(@"lat:%f lon:%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
//Debug(@"did fail with error. stop updating and return error.");
[self.view setBackgroundColor:[UIColor orangeColor]];
}
@end
我加了
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs your location to provide the best service.</string>
到 Info.plist 文件,仍然...没有变化,位置图标出现在电池附近的状态栏上,但没有位置,无论是真实的还是模拟的。
我是不是漏掉了什么?
尝试添加更改授权方法:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
// This method has to be here to trigger the UIAlertView
switch (status) {
case kCLAuthorizationStatusDenied:
break;
case kCLAuthorizationStatusRestricted:
break;
case kCLAuthorizationStatusNotDetermined: {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[self.locationManager requestAlwaysAuthorization];
}
default:
break;
}
}
编辑
您使用了错误的委托方法。 didUpdateToLocation 已被弃用。改用这个:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {}
Core Location 用法最近发生了变化,但即使更新到新规范后,我似乎也无法正常工作。
我尝试用一个视图控制器开始一个新项目,该视图控制器在 GPS 坐标查找或失败时更改其背景颜色。
#import "ViewController.h"
@interface ViewController (){
CLLocationManager *locationManager;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (locationManager == nil)
{
locationManager = [[CLLocationManager alloc] init];
}
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if (![CLLocationManager locationServicesEnabled]) {
[self.view setBackgroundColor:[UIColor redColor]];
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark CLLocationManagerDelegate
//
//
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
{
[self.view setBackgroundColor:[UIColor greenColor]];
NSLog(@"lat:%f lon:%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
//Debug(@"did fail with error. stop updating and return error.");
[self.view setBackgroundColor:[UIColor orangeColor]];
}
@end
我加了
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs your location to provide the best service.</string>
到 Info.plist 文件,仍然...没有变化,位置图标出现在电池附近的状态栏上,但没有位置,无论是真实的还是模拟的。
我是不是漏掉了什么?
尝试添加更改授权方法:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
// This method has to be here to trigger the UIAlertView
switch (status) {
case kCLAuthorizationStatusDenied:
break;
case kCLAuthorizationStatusRestricted:
break;
case kCLAuthorizationStatusNotDetermined: {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[self.locationManager requestAlwaysAuthorization];
}
default:
break;
}
}
编辑
您使用了错误的委托方法。 didUpdateToLocation 已被弃用。改用这个:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {}