【问题标题】:didUpdateLocations delegate method not call in iOS 5.1 and earlier在 iOS 5.1 及更早版本中不调用 didUpdateLocations 委托方法
【发布时间】:2013-07-22 00:43:37
【问题描述】:

我正在使用 CoreLocation 框架在我的项目中获取纬度和经度。

我已经在我的 .m 文件中实现了这个委托方法

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

   NSLog(@"Locations : %@", locations); 
}

但是在 iOS 5.0 & 5.1 中,这个方法是不能调用的。 在 iOS 6.0 或更高版本中它可以正常调用。 那么如何在 iOS 5.0 或更高版本中调用此方法。

【问题讨论】:

标签: iphone objective-c ios5 core-location cllocationmanager


【解决方案1】:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 

上述委托方法在 iOS 6 中已弃用。

代替这个,使用:

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

此代码(以及 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;
}

更多细节在这里:

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

【讨论】:

  • 已弃用并不意味着它不存在。它仍然存在并且仍然有效。已弃用意味着它将在未来的某个版本中消失。
  • @progrmr :您以错误的方式获得它。我写这个是为了说明,如果我们使用该委托方法,这不是正确的方法。我的代码可以工作,但只是由于我的英语不好,你否决了我的回答,我认为这不公平。
  • 我投了反对票,因为检查版本号不是检查可用功能的推荐方法。通常最好检查对象是否响应选择器。
【解决方案2】:

didUpdateLocations: 在 ios 5 或更早版本中不存在。如果您想支持这些 ios 版本,则只需使用已弃用的 didUpdateToLocation:fromLocation。即使它已被弃用,它仍然适用于 ios 6 和 7。这是最简单的解决方案。

您可以为这两种方法设置委托方法,但管理起来会更加复杂。如果您不需要在后台构建跟踪日志,那么您真的不需要didUpdateLocations: 它是为了节省电池电量而在后台运行时收集多个点。

【讨论】:

    【解决方案3】:

    参考:

    http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html

    如您所见,locationManager:didUpdateLocations: 仅在 iOS 6.0 及更高版本中可用。

    您始终可以从

    获取最近更新的位置
    CLLocationManger's location 
    

    财产。

    【讨论】:

      【解决方案4】:

      查看此答案...此方法适用于 iOS 5(iOS 6 已弃用)和旧版本。

      - (void)locationManager:(CLLocationManager *)manager 
          didUpdateToLocation:(CLLocation *)newLocation 
                 fromLocation:(CLLocation *)oldLocation
      

      此方法适用于 iOS 6 及更高版本..

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-13
        • 1970-01-01
        • 1970-01-01
        • 2016-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-07
        相关资源
        最近更新 更多