核心位置在iOS 8中不起作用

cha*_*eyh 8 cocoa-touch core-location ios ios8

我正在尝试在iOS 8中使用重要的更改位置监视,但从未调用didUpdateLocations方法.以下是设置位置管理器的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    [locationManager requestWhenInUseAuthorization];
    [locationManager startMonitoringSignificantLocationChanges];
}
Run Code Online (Sandbox Code Playgroud)

尽管打电话requestWhenInUseAuthorization,但没有任何东西弹出来要求用户授权它.我已经设置了NSLocationWhenInUseUsageDescription,它仍然无效.didChangeAuthorizationStatus并且didFailWithError也从未被称为.编辑:我能够让它要求用户允许位置服务,但即使你点击允许它从不显示位置.

Mar*_*tin 17

目前iOS8 beta5中存在一个错误,它总是会停用您应用的地理定位服务(至少对我而言).

进去settings > Privacy > Location services > Your app > Always.

但我不知道为什么,但即使您将其Always设置为此设置也会自动停用,因此请耐心等待并经常返回设置以再次配置您的应用位置.


moh*_*acs 11

尝试CLLocationManager *locationManager在头文件中声明然后它可以工作.除非我已将其声明为全局变量,否则它不会要求权限和更新位置.

.H

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
}
Run Code Online (Sandbox Code Playgroud)

.M

- (void)viewDidLoad {
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:(id)self];
    [locationManager requestWhenInUseAuthorization];
    [locationManager startMonitoringSignificantLocationChanges];
    [locationManager startUpdatingLocation];
}
Run Code Online (Sandbox Code Playgroud)

委托方法

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    NSLog(@"Getting Location");
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"%@", error.localizedDescription);
}
Run Code Online (Sandbox Code Playgroud)

info.plis

在此输入图像描述


bha*_*vik 5

到 .plist 文件中添加 在此处输入图片说明

locationManager = [[CLLocationManager alloc] init];
            [locationManager setDelegate:self];
            [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];

if(IS_OS_8_OR_LATER)
    {
 if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
            {
                [locationManager requestWhenInUseAuthorization];
            }
}

[locationManager startUpdatingLocation];


 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
     currentLocation = [locations lastObject];
    }
Run Code Online (Sandbox Code Playgroud)