iOS 8 gps not enabled

大兔子大兔子 提交于 2019-12-01 03:01:24

问题


I tackled an issue, that GPS services work perfectly on iOS 7, but on iOS 8 I never get the permission request and the method:

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

never gets called.

My code is here:

#import "Locator.h"

@implementation Locator

- (instancetype) init {
    if (self = [super init]) {
        // Start up the location manager
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = 3;
        // New property for iOS6
        if ([self.locationManager respondsToSelector:@selector(activityType)]) {
            self.locationManager.activityType = CLActivityTypeFitness;
        }
        // New method for iOS8
        if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.locationManager requestAlwaysAuthorization];
        }
    }
    return self;
}

- (void) startMonitoring:(LocationChangeCallback)callback {
    if ([CLLocationManager locationServicesEnabled] && [CLLocationManager significantLocationChangeMonitoringAvailable]) {
        // Register an observer for if/when this app goes into background & comes back to foreground
        // NOTE: THIS CODE IS iOS4.0+ ONLY.
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchToLowEnergyMode) name:UIApplicationDidEnterBackgroundNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchToAccurateMode) name:UIApplicationDidFinishLaunchingNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchToAccurateMode) name:UIApplicationWillEnterForegroundNotification object:nil];

        UIApplicationState state = [[UIApplication sharedApplication] applicationState];

        self.locationUpdateCallback = callback;

        if (state == UIApplicationStateActive) {
            [self switchToAccurateMode];
        } else {
            [self switchToLowEnergyMode];
        }
    }
}

- (void) switchToAccurateMode {
    NSLog(@"Accurate");
    [self.locationManager stopMonitoringSignificantLocationChanges];

    // Find the current location
    [self.locationManager startUpdatingLocation];
}

- (void) switchToLowEnergyMode {
    NSLog(@"Low Energy");
    [self.locationManager stopUpdatingLocation];

    // Find the current location
    [self.locationManager startMonitoringSignificantLocationChanges];
}


#pragma mark - CLLocationDelegate Methods

- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    // locations contains an array of recent locations, but this app only cares about the most recent
    // which is also "manager.location"
    if (self.locationUpdateCallback != nil) {
        self.locationUpdateCallback(manager.location);
    }
}

- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Location manager failed with error: %@", error);
    if ([error.domain isEqualToString:kCLErrorDomain] && error.code == kCLErrorDenied) {
        //user denied location services so stop updating manager
        [manager stopUpdatingLocation];
    }
}
@end

I also checked under location settings and there was nothing behind my application name. Other applications have ("Always", "Never" or "While Using").


回答1:


For IOS 8 you have to add NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key to your plist. Otherwise it does not ask for permission.

add CLLocationManagerDelegate

@implementation Locator <CLLocationManagerDelegate>

- (instancetype) init {
    //......
    self.locationManager.requestAlwaysAuthorization();
}


来源:https://stackoverflow.com/questions/24821156/ios-8-gps-not-enabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!