【问题标题】:Core Location not working in iOS 8核心位置在 iOS 8 中不起作用
【发布时间】:2014-09-12 13:01:33
【问题描述】:

我正在尝试在 iOS 8 中使用重大更改位置监控,但从未调用过 didUpdateLocations 方法。这是设置位置管理器的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    [locationManager requestWhenInUseAuthorization];
    [locationManager startMonitoringSignificantLocationChanges];
}

尽管调用requestWhenInUseAuthorization,但没有弹出请求用户授权。我已经设置了 NSLocationWhenInUseUsageDescription,但它仍然不起作用。 didChangeAuthorizationStatus 和 didFailWithError 也永远不会被调用。 编辑:我能够让它要求用户允许位置服务,但即使你点击允许它也不会显示位置。

【问题讨论】:

  • 这主要是因为人们没有将 NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription 添加到 info.plist 文件中,但您说您已经拥有它,那么 CLLocationManagerDelegate 呢。 iOS 8 gps not enabled
  • 我将委托设置为 self,并且我在标题中有 。
  • @charleyh:我面临同样的问题。你有什么解决办法吗?
  • 我在这里发布了关于 iOS 8 中位置管理器的一些更改:nevan.net/2014/09/core-location-manager-changes-in-ios-8
  • 在这种特殊情况下(至少在编辑之前),原因可能是位置管理器在内存中保存的时间不够长。请参阅下面的答案。

标签: ios cocoa-touch core-location ios8


【解决方案1】:

目前 iOS8 beta5 中存在一个错误,它总是会为您的应用(至少对我的应用)停用地理定位服务。

进入settings > Privacy > Location services > Your app > Always。

但我不知道为什么,但即使你将其设置为Always,此设置也会自动停用,所以请耐心等待并经常返回设置以重新配置您的应用位置。

【讨论】:

  • 我发现应用商店中的应用运行良好,但 adhoc 却不行。
  • 不客气,但我不明白你怎么还会遇到这个错误,因为最终的 ios8 版本已经可用。
  • @Developer 您确定不同行为的原因是临时与应用商店吗?我认为区别在于使用 XCode 5 和 XCode 6 进行编译
  • @GeorgeSachpatzidis 我发现问题是我没有要求 requestWhenInUseAuthorization。这是他们在 iOS 8 中强制要求的
【解决方案2】:

尝试在你的头文件中声明CLLocationManager *locationManager 然后它就可以工作了。除非我将其声明为全局变量,否则它不会请求许可和更新位置。

.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
}

.m

- (void)viewDidLoad {
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:(id)self];
    [locationManager requestWhenInUseAuthorization];
    [locationManager startMonitoringSignificantLocationChanges];
    [locationManager startUpdatingLocation];
}

委托方法

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    NSLog(@"Getting Location");
}

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

info.plis

【讨论】:

  • 这仍然不适合我。弹出窗口,我点击允许,但它实际上并没有给出位置。
  • 是模拟器还是设备?如果是模拟器,可能是菜单/调试/位置中未选择位置,应打开 wifi
  • 它在模拟器上,但我在调试菜单中设置了一个位置。
  • 这是一个错误 - 即使模拟器上的地图应用程序也不会显示我的位置。
  • 模拟器无法找到您的位置。上面的代码对我来说在 Xcode 6 beta 4 中运行良好。
【解决方案3】:

到 .plist 文件中添加

locationManager = [[CLLocationManager alloc] init];
            [locationManager setDelegate:self];
            [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];

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

[locationManager startUpdatingLocation];


 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
     currentLocation = [locations lastObject];
    }

【讨论】:

    【解决方案4】:

    所有步骤看起来都已完成,但只有在检查 :didChangeAuthorizationStatus 后才运行 :startUpdatingLocation。

    此时,请确保 authorizationStatus 不是 kCLAuthorizationStatusNotDetermined 或 kCLAuthorizationStatusDenied。

    如果是,请检查 info.plist 中的字符串条目。

    如果您认为所有内容都正确,请确保从设备上卸载该应用程序。 执行干净的构建,然后在设备上运行应用程序。

    【讨论】:

    • 是的,模拟器中的“重置内容和设置”并重建/运行应用程序解决了问题
    【解决方案5】:

    我在将我的应用程序迁移到 iOS 8 时遇到了类似的问题。 作为基于Apple提供的示例代码的原始代码 https://developer.apple.com/library/ios/samplecode/GeocoderDemo/Introduction/Intro.html 我检查了它是否已更新并且已经更新。主要区别在于此功能与 iOS 8 相关的位已被注释。这对我有用。

    - (void)startUpdatingCurrentLocation
    {
     // if location services are restricted do nothing
        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || 
           [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted )
        {
           return;
        }
    
    // if locationManager does not currently exist, create it
       if (self.locationManager == nil)
       {
            _locationManager = [[CLLocationManager alloc] init];
            [self.locationManager setDelegate:self];
            self.locationManager.distanceFilter = 10.0f; // we don't need to be any more accurate than 10m
       }
    
       // for iOS 8, specific user level permission is required,
       // "when-in-use" authorization grants access to the user's location
       //
      // important: be sure to include NSLocationWhenInUseUsageDescription along with its
      // explanation string in your Info.plist or startUpdatingLocation will not work.
      //
      if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
      {
          [_locationManager requestWhenInUseAuthorization];
      }
    
      [_locationManager startUpdatingLocation];
    
      [self showCurrentLocationSpinner:YES];
    }
    

    【讨论】:

      【解决方案6】:

      试试

      1. 后台模式开启

      2. #import &lt;CoreLocation/CoreLocation.h&gt;

      3. &lt;CLLocationManagerDelegate&gt;

      4. locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:(id)self]; [locationManager requestWhenInUseAuthorization];

        那是在应用程序的开头

      5. NSLocationWhenInUseUsageDescription 在 plist 中

      6. 试试 CMD + SHIFT + K

      7. 尝试关闭 iPhone,然后再打开 - 他在 iOS 8 中有一些现金 - 所以 5 次后 - 应用程序不请求 Core Location 的许可 - 这对我有帮助

      【讨论】:

        【解决方案7】:

        授权对话框未与 OPs 代码一起显示的可能原因是位置管理器必须等待它才能工作。

        使用属性或静态变量将locationManager 保存在内存中。

        【讨论】:

          【解决方案8】:

          可能是因为 WhenInUseAuthorization 不足以进行“重大位置变化”更新。

          当我使用 AlwaysAuthorization 时,它可以正常工作。

          我在文档中找不到它。但您可以查看http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/。在“授权类型”标题下,它指出重要的位置更新需要 AlwaysAuthorization。

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-30
          • 2014-12-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-28
          • 2014-11-14
          • 1970-01-01
          相关资源
          最近更新 更多