iOS8核心位置有问题

时间:2015-02-12 14:43:19

标签: ios objective-c ios8 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文件,但仍然......没有变化,位置图标出现在电池附近的状态栏上,但没有位置,真实或模拟。

我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

尝试添加更改授权方法:

- (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 {}