【问题标题】:iOS 6 CoreLocation does not workiOS 6 CoreLocation 不起作用
【发布时间】:2012-09-25 04:16:43
【问题描述】:

我制作了一个位置应用程序。但核心位置不起作用。 我在我的 iPhone 上测试了其他 iPhone 应用程序。
像google earth,一个导航软件。 其他应用程序也不起作用。
为什么不更新位置?
为什么 'locationManager:didUpdateToLocation:fromLocation:' 消息只调用了 2 次?

也许……我的 iPhone 坏了?还是 iOS 6 CoreLocation 框架有一些错误?

定位服务 - 在 iPhone 设置上开启

Info.plist

  • armv7
  • 加速度计
  • 定位服务
  • 全球定位系统
  • 麦克风
  • 磁力计

代码示例:

- (CLLocationManager *)setupLocationManager
{
  if ([CLLocationManager locationServicesEnabled] && [CLLocationManager headingAvailable]) {

     CLLocationManager *locationManager = [[CLLocationManager alloc] init];
     locationManager.delegate = self;
     locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
     locationManager.distanceFilter = kCLDistanceFilterNone;
     locationManager.headingFilter = kCLHeadingFilterNone;
     [locationManager startUpdatingLocation];
     [locationManager startUpdatingHeading];
     return locationManager;
  }
  return nil;
}

- (CLLocationManager *)locationManager
{
  switch([CLLocationManager authorizationStatus])
  {
    case kCLAuthorizationStatusAuthorized:
      _deltaTimeLocationReceived = 0.0;
      if (_locationManager == nil)
        _locationManager = [self setupLocationManager];
       return _locationManager;

      case kCLAuthorizationStatusDenied:
      case kCLAuthorizationStatusRestricted:
        if (_locationManager)
          _locationManager = nil;
        return _locationManager;

      case kCLAuthorizationStatusNotDetermined:
        _deltaTimeLocationReceived = 0.0;
        if (_locationManager == nil)
          _locationManager = [self setupLocationManager];
        return nil;
    }
    return nil;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
  NSLog(@"%@ %@", NSStringFromSelector(_cmd), newLocation.description); 
  if (self.locationManager) _locationSignal++;
}

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

【问题讨论】:

  • 您是否清除了安全设置?
  • 是的,我设置了定位服务 - 开启。谢谢。
  • 设置 -> 常规 -> 重置 -> 重置位置和隐私。
  • 感谢 jessedc。我确实重置了位置和隐私。但我的 iPhone 不工作 TT。
  • 问题不在 iOS 6 中。CLLocationManager 适用于许多应用程序。

标签: iphone ios ios6 core-location


【解决方案1】:

您有任何来自委托方法的消息吗?

如果没有消息,检查你的类的接口描述。

@interface ... <... cllocationmanagerdelegate ...>

【讨论】:

    【解决方案2】:

    在 iOS 6 中,Apple 通过实现 AutoPause API 对核心位置进行了重大更改。 AutoPause API 在应用程序进入后台时暂停位置更新并适用于几个标准(即用户不移动、没有位置修复、用户停止活动)。为了准确处理 Apple 请求的暂停事件,以帮助更好地预测是否暂停位置更新,设置活动类型(即导航、健身等)。 在针对 iOS 6 编译应用程序时,默认情况下会启用 AutoPause API。

    简单的解决方法是暂时禁用 AutoPause API,方法是始终将“pausesLocationUpdatesAutomatically”设置为 NO。即使应用程序在后台运行,也会发送位置更新,就像它过去在

    更多细节在这里:

    http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

    【讨论】:

    • 我添加代码“pausesLocationUpdatesAutomatically = NO”。我有需要。
    • 但是你见过调用自动暂停回调吗?
    【解决方案3】:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 委托不再存在于 ios 6 中。

    改为使用

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations
    

    查看Apple documentation for startUpdateLocation

    此代码(以及 pausesLocationUpdatesAutomatically)只能在 XCode 4.5(其中基础 SDK 为 6)中编译。

    如果您想定位 6.0 之前的 iOS 版本,请使用此宏

    #define IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
    

    以及在何处创建 CLLocationManager 对象

    if(IOS_VERSION_GREATER_THAN_OR_EQUAL_TO (@"6.0"))
    {
        locationManagerObj.pausesLocationUpdatesAutomatically = NO;
    }
    

    当从 Xcode 4.5 编译时,locationManager 委托应该在所有版本中工作

    【讨论】:

    • *我的意思是所有支持的版本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2015-01-22
    • 2012-09-15
    相关资源
    最近更新 更多