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

标签 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 之前可以工作。

编辑:

当在新的测试 View Controller 中执行用户 IOS 提供的代码时,我得到了相同的结果。这是显示正在调用的屏幕截图。 (我将调试器单步执行到该行,从您可以看到断点的地方开始) shows that line is called

最佳答案

如果您正在获取 '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];
    }
}

关于ios - CLLocation 服务。添加了 plist key 和请求权限,但仍然不适用于 IOS 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29806206/

相关文章:

ios - 如何将从该请求收到的数据或 json 附加到空数组?

iphone - 为什么我的 UILabel 没有居中?

ios - 保存 UICollectionViewCell 进度状态

ios - 是否可以使用 iOS Facebook SDK 检索用户的_whole_好友列表?

iphone - 为什么在定义方法后会出现 "unrecognised selector sent to instance"错误?

ios - 我是否需要将AWS S3或EC2用于我的iOS应用程序的后端?

objective-c - SdWebImage 下载缩略图然后下载高分辨率

ios8 - 切换到arm64时打包出错(arm5还可以)

ios - iOS 8 中的自定义键盘可以使用新字符吗?

ios - 我可以在我的应用程序中获取其他应用程序的打开关闭回调吗