【问题标题】:Location manager is not working in iOS8位置管理器在 iOS8 中不起作用
【发布时间】:2015-04-25 22:26:08
【问题描述】:

请不要将其标记为重复,因为我从堆栈溢出中发布的其他人那里获得了帮助。但我仍然面临一些问题。即使在真实设备中也不会调用位置管理器委托。而且它也没有请求许可。

下面是我的代码。

- (IBAction)getUserLocationAction:(id)sender
{
    if([CLLocationManager locationServicesEnabled])
    {
        if(!locationManager)
            locationManager = [[CLLocationManager alloc] init];

        [locationManager setDelegate:self];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

        if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [locationManager requestWhenInUseAuthorization];
        }

        [locationManager startUpdatingLocation];
        //[locationManager startMonitoringSignificantLocationChanges];
    }
    else
    {
        NSLog(@"Location service is not enabled");
    }
}


#pragma mark - Location Manager Delegate- 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"%@", locations);
    userLocation = [locations lastObject];
    NSLog(@"User Current Locatiion\nLat: %+.6f\nLong: %+.6f", [userLocation coordinate].latitude, [userLocation coordinate].longitude);

   [locationManager stopUpdatingLocation];
   //[locationManager stopMonitoringSignificantLocationChanges];
}



- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"Error: %@", [error localizedDescription]);
}

我还在我的 .plist 文件中添加了以下两个键

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app want to use your location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app want to use your location</string>

我还添加了框架

我不确定我在哪里做错了。请帮帮我。

更新

首先,我认为这是我的代码的问题,但经过一些研究,它在 iOS7 中按预期工作。代码保持不变,此问题仅在 iOS8 中出现。请让我知道在 iOS8 中工作还需要做哪些更改。

【问题讨论】:

  • 当用户接受您的应用使用该位置时,您在哪里处理回调?
  • 它没有请求任何许可。
  • [locationManager requestWhenInUseAuthorization];这一行设置断点,看是否被调用。
  • 是的,它被调用了。
  • 那你应该参考属性self.locationManager,而不是iVar locationManager

标签: ios iphone delegates core-location cllocationmanager


【解决方案1】:

你需要做更多的检查:

- (IBAction)getUserLocationAction:(id)sender
{
    if([CLLocationManager locationServicesEnabled]) {

        if(!self.locationManager) {
            self.locationManager = [[CLLocationManager alloc] init];
            [self.locationManager setDelegate:self];
            [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        }

        CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
        if (authStatus == kCLAuthorizationStatusNotDetermined) {
            // Check for iOS 8 method
            if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [self.locationManager requestWhenInUseAuthorization];
            }
            else {
                [self.locationManager startUpdatingLocation];
            }
        }
        else if(authStatus == kCLAuthorizationStatusAuthorizedAlways || authStatus == kCLAuthorizationStatusAuthorizedWhenInUse || authStatus == kCLAuthorizationStatusAuthorized) {
            [self.locationManager startUpdatingLocation];
        }
        else if(authStatus == kCLAuthorizationStatusDenied){
            NSLog(@"User did not allow location tracking.");
            // present some dialog that you want the location.
        }
        else {
            // kCLAuthorizationStatusRestricted
            // restriction on the device do not allow location tracking.
        }
    }
    else {
        NSLog(@"Location service is not enabled");
    }
}

还需要处理使用回调:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status != kCLAuthorizationStatusRestricted &&  status !=kCLAuthorizationStatusDenied) {
        [self.locationManager startUpdatingLocation];
    }
}

【讨论】:

  • 我已经改变了你在getUserLocationAction 中发布的内容。它没有记录任何内容。它被称为didChangeAuthorizationStatusstatus = kCLAuthorizationStatusNotDetermined 即将到来。
  • 仅在 iOS8 中遇到此问题,在 iOS7 中工作正常
【解决方案2】:

检查你的位置访问设置..所以去设置-->隐私-->定位服务-->你的应用

【讨论】:

  • 我的应用不在Setting--&gt;Privacy--&gt;Location Services下列出
  • k....我认为您应该检查此链接nevan.net/2014/09/core-location-manager-changes-in-ios-8,简要描述 ios 8 中的位置服务更改
  • 我也从这个站点获得了帮助。如果你能检查我的代码,你就会明白。我觉得我错过了一些东西,因为我的代码看起来不错,而且 MidhunMP 通过使用我的代码获得了成功。天知道会发生什么。
猜你喜欢
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 2015-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多