iOS 8 gps未启用 [英] iOS 8 gps not enabled

查看:115
本文介绍了iOS 8 gps未启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解决了一个问题,即GPS服务在iOS 7上完美工作,但在iOS 8上我从未获得权限请求和方法:

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



<



我的代码在这里:

  #importLocator.h

@implementation Locator
$ b - (instancetype)init {
if(self = [super init]){
//启动位置管理器
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 3;
// iOS 6的新属性
if([self.locationManager respondsToSelector:@selector(activityType)]){
self.locationManager.activityType = CLActivityTypeFitness;
}
//为iOS8添加新方法
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[self.locationManager requestAlwaysAuthorization];
}
}
返回自我; (CLLocationManager locationServicesEnabled]&< [CLLocationManager significantLocationChangeMonitoringAvailable]){
//(void)startMonitoring:(LocationChangeCallback)callback {
if注册观察员,了解此应用进入后台的时间和时间返回到前台
//注意:此代码仅适用于iOS4.0 +。
[[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];



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

//找到当前位置
[self.locationManager startUpdatingLocation];
}

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

//查找当前位置
[self.locationManager startMonitoringSignificantLocationChanges];


$ b #pragma mark - CLLocationDelegate方法
$ b - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// locations包含一组最近的位置,但是这个app只关心最近的
//这也是manager.location
if(self.locationUpdateCallback!= nil) {
self.locationUpdateCallback(manager.location);


$ b - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@Location manager failed and error :%@,错误);
if([error.domain isEqualToString:kCLErrorDomain]&& error.code == kCLErrorDenied){
//用户拒绝位置服务,所以停止更新管理器
[manager stopUpdatingLocation];
}
}
@end

设置,我的应用程序名称背后没有任何东西。其他应用程序有(Always,Never或While Using)。

NSLocationAlwaysUsageDescription NSLocationWhenInUseUsageDescription 键给你的plist。

添加CLLocationManagerDelegate

  @实现Locator< CLLocationManagerDelegate> 

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


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").

解决方案

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();
}

这篇关于iOS 8 gps未启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆