【问题标题】:iOS 8 gps not enablediOS 8 GPS 未启用
【发布时间】:2014-07-18 09:03:58
【问题描述】:

我解决了一个问题,GPS 服务在 iOS 7 上完美运行,但在 iOS 8 上我从未收到权限请求和方法:

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

永远不会被调用。

我的代码在这里:

#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

我还检查了位置设置,我的应用程序名称后面没有任何内容。其他应用程序有(“始终”、“从不”或“使用时”)。

【问题讨论】:

    标签: ios objective-c gps cllocationmanager


    【解决方案1】:

    对于 IOS 8,您必须将 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 密钥添加到您的 plist。否则它不会请求许可。

    添加 CLLocationManagerDelegate

    @implementation Locator <CLLocationManagerDelegate>
    
    - (instancetype) init {
        //......
        self.locationManager.requestAlwaysAuthorization();
    }
    

    【讨论】:

    • 如果您只需要在应用运行时(而不是在后台)访问 LocationManager,您也可以调用 requestWhenInUseAuthorization 并将 NSLocationWhenInUseUsageDescription 添加到您的 plist 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多