CLLocation 服务。添加了 plist 密钥和请求权限,但仍然不适用于 IOS 8

CLLocation services. Added plist keys and request permission, yet still not working for IOS 8

我检查了有关堆栈溢出的其他主题并进行了相同的更改。 Apple 文档似乎也没有我遗漏的任何内容。

从我的 plist 文件中复制:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    ...
    <key>NSLocationUsageDescription</key>
    <string>Used for finding users in your area. &quot;Stealth mode&quot; can be used if you would like not users to see your location on the map</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Used for finding users in your area. &quot;Stealth mode&quot; can be used if you would like not users to see your location on the map</string>
    ...
</dict>
</plist>

我的代码:

+(AsyncBackgroundUpdateManager*)getInstance
{
    if (instance == nil)
    {
        instance = [[AsyncBackgroundUpdateManager alloc] init];
    }

    return instance;
}

-(id)init
{
    if (instance != nil)
    {
        NSLog(@"WARNING: You should not call init on an AsyncBackgroundUpdateManager, you should use the static \"getInstance\" method.@");
    }

    self = [super init];
    if (self)
    {
        if ([CLLocationManager locationServicesEnabled])
        {
            if (locManager == nil)
            {
                locManager = [[CLLocationManager alloc] init];
            }

            locManager.delegate = self;
            locManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
            locManager.distanceFilter = 100;//Meters
        }
        else
        {
            NSLog(@"Location not enabled");
        }
    }

    return self;
}


// -------------------------------------------------------------
// Controlling updating process
// -------------------------------------------------------------

-(void)start
{
    //Get locations permission
    if ([locManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [locManager requestWhenInUseAuthorization];
        switch ([CLLocationManager authorizationStatus]) {
            case kCLAuthorizationStatusDenied:
                NSLog(@"Location services are not allowed");
                break;
            case kCLAuthorizationStatusNotDetermined:
                NSLog(@"Location services are undecided");
                break;
            case kCLAuthorizationStatusAuthorizedWhenInUse:
            case kCLAuthorizationStatusAuthorized:
                NSLog(@"Location services are allowed");
                break;
            case kCLAuthorizationStatusRestricted:
                NSLog(@"Location services restricted?");
                break;
        }

    }
    [locManager startUpdatingLocation];
    theTimer = [NSTimer scheduledTimerWithTimeInterval:120 target:self selector:@selector(doUpdate) userInfo:nil repeats:YES];
}

线路呼叫:

[[AsyncBackgroundUpdateManager getInstance] start];

我已经验证位置管理器是通过 alloc/init 行上的断点初始化的。

我已经通过在该行设置断点来验证调用了 requestWhenInUseAuthorization。

我从未收到定位服务提示,也从未调用过更新位置中的断点。

在我的设备设置中,我的应用似乎没有请求位置权限。

这是输出:定位服务未定

编辑:

来自评论: start方法在主线程中被调用 我 运行 使用的设备启用了位置服务,并且是 运行 ios 8。这在 ios 8.

之前有效

编辑:

在新的测试视图控制器中执行用户 IOS 提供的代码时,我得到了相同的结果。这是显示正在调用呼叫的屏幕截图。 (我将调试器步进到那一行,从你可以看到断点的地方开始)

如果您的状态为 'kCLAuthorizationStatusNotDetermined,则您的应用尚未设置定位服务。您需要在调用 startUpdatingLocation

之前获得许可

尝试这样的事情:

//request permissions
if([myLocationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
    //iOS 8 and above permission request
    //get current status
    CLAuthorizationStatus currentStatus=[CLLocationManager authorizationStatus];

    //if undetermined, ask for permission
    if (currentStatus == (kCLAuthorizationStatusNotDetermined)) {
        //request permission
        [myLocationManager requestWhenInUseAuthorization];
    }
}

我的项目有一个 InfoPlist.strings 文件。 这是对普通 AppName-Info.plist 文件的补充。

将密钥输入普通 plist 文件没有任何作用,尽管它使我通过了用户 IOS 和其他用户提供的测试。我必须将密钥输入 InfoPlist.strings 文件。

快速查看苹果文档后:https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html

InfoPlist.strings 文件似乎是规范化的一部分。您有基于语言的同一文件的多个版本。正如苹果所说:

The routines that look up key values in the Info.plist file take the user’s language preferences into account and return the localized version of the key (from the appropriate InfoPlist.strings file) when one exists. If a localized version of a key does not exist, the routines return the value stored in the Info.plist file.

因此,在我看来,此功能似乎并没有按照我所理解的方式工作...

但是,无论如何,将密钥添加到我的 Info.plist 和我的 InfoPlist.strings 文件似乎可以解决问题。

编辑: 就我所经历的行为而言,我似乎是不正确的。

我的 InfoPlist.strings 文件包含一个旧密钥:

<key>NSLocationUsageDescription</key>
<string>Used for finding users in your area. &quot;Stealth mode&quot; can be used if you would like not users to see your location on the map</string>

添加这两个键导致它出错,并且在将它们放在原始 Info.plist 文件中的同时删除这两个键修复了它。

我的编译器似乎在我的 mac 上有问题。在另一个项目中,class 未与其父项 class 一起正确编译。我一遍又一遍地构建和重建,但我不知道为什么 class 没有显示为它应该显示的父项的子 class。清理项目并重建修复它。

这个问题可能与此有关。