ios - iOS8 核心位置有问题

标签 ios objective-c ios8 core-location

Core Location 的使用最近发生了变化,但即使在更新到新规范后我似乎也无法让它工作。

我尝试只使用一个 View Controller 开始一个新项目,该 View Controller 会在 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;
    }
}

编辑

您使用了错误的委托(delegate)方法。 didUpdateToLocation 已被弃用。改用这个:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {}

关于ios - iOS8 核心位置有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28480363/

相关文章:

iphone - 为什么我不能注册我的 iOS 应用程序来处理图像文件类型?

iphone - 从 UIAlertView 获取文本

ios - 如何导出在设备中为 Xcode 6 创建的文件?

ios - UIVisualEffectView 在不同设备上呈现不同

ios - 将 Cocoapods 链接为共享的 iOS 8 库

objective-c - 多部分 POST 和 objective-c

ios - 如何通过这个错误来调试我的代码登录IOS

objective-c - 是否可以在不使用 AVAudioSessionCategoryPlayAndRecord 的情况下同时播放和录制音频

iOS 框架概念 : QR Code Scanning

ios - 为什么delegate.respondsToSelector(Selector ("testEnum:"))这段代码用swift语言会返回false?