我检查了有关堆栈溢出的其他主题并做了相同的更改。 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. "Stealth mode" 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. "Stealth mode" 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 提供的代码时,我得到了相同的结果。这是显示正在调用的屏幕截图。 (我将调试器单步执行到该行,从您可以看到断点的地方开始)
最佳答案
如果您正在获取 '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/