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

时间:2015-04-22 18:39:31

标签: ios objective-c ios8 plist cllocationmanager

我已经检查了堆栈溢出的其他主题并完成了相同的更改。 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提供的代码时,我得到相同的结果。这是一个屏幕截图,显示正在调用该调用。 (我将调试器调到该行,从可以看到断点开始) shows that line is called

2 个答案:

答案 0 :(得分:1)

如果您的状态为'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];
    }
}

答案 1 :(得分:0)

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

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

快速浏览一下apple docs:https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html

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

  

在Info.plist文件中查找键值的例程采用   将用户的语言首选项纳入帐户并返回本地化   密钥的版本(来自相应的InfoPlist.strings文件)时   一个存在。如果密钥的本地化版本不存在,则   例程返回存储在Info.plist文件中的值。

所以,在我看来,根据所说的内容,我的理解功能并不像我理解的那样......

但是,无论如何,将密钥添加到我的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上有问题。在另一个项目中,一个类没有用它的父类正确编译。我一遍又一遍地建造和重建,我不知道为什么这个课程没有显示为它应该具有的父母的子类。清理项目,并重建修复它。

这个问题可能与此有关。