【问题标题】:StartMonitoringSignificantLocationChanges not calling didupdatedLocationsStartMonitoringSignificantLocationChanges 未调用 didupdateLocations
【发布时间】:2014-06-15 16:23:53
【问题描述】:

我正在开发一个使用位置信息的 iOS 7 应用程序。我从来没有更新位置。如果我对startUpdatingLocation 做同样的事情,效果很好。我已经阅读了文档,但我不知道问题出在哪里。 该应用程序已启用该位置,它会在我第一次运行该应用程序时询问您是否要启用它。状态栏中的位置图标也不显示。

更新:在它工作的模拟器中,它给了我一个更新的位置。为什么它在设备中不起作用?

它只会在一个类中使用,这里是配置。

#import <CoreLocation/CoreLocation.h>

@interface ClassLocation : UIViewController <CLLocationManagerDelegate>{
} 

在 .h 中设置属性,在 .m 中设置合成

@property (strong, nonatomic) CLLocationManager *localizacionManager;

@synthesize localizacionManager;

ViewDidLoad:

localizacionManager=[[CLLocationManager alloc] init];
[localizacionManager setDelegate:self];
[localizacionManager setDesiredAccuracy:kCLLocationAccuracyKilometer];
[localizacionManager startMonitoringSignificantLocationChanges];

还有两个委托方法:

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"error: %@",error.description);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    NSLog(@"Location updated");
}

【问题讨论】:

  • didUpdateLocations: 方法真的从未被调用过,或者可能只被调用过一次,然后就不再被调用了吗?该方法最多每 5 分钟调用一次,所以这可能是原因。此外,您的手机在测试时是否会显着改变其位置?如果没有,可以使用设备模拟器模拟位置变化(Simulator menu --> Debug --> Location)
  • 它从未被调用,因为我放置了一个 NSLog 和一个断点,它从未被调用过。我不会更改我的位置,但它至少应该调用一次“didUpdateLocations”,正如 Apple 文档所述
  • 是的,没错,这个方法通常一开始至少调用一次,然后需要一段时间(大约5分钟)才能再次调用,这确实很奇怪!
  • 在模拟器中试了一下,效果很好。那为什么不在设备中呢?感谢您的回答尼古拉斯!
  • @saimonx 你解决了这个问题吗?

标签: ios objective-c ios7 cllocationmanager


【解决方案1】:

将密钥添加到您的 Info.plist 并请求位置管理器授权以启动它。

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

您需要为相应的定位方式申请授权。

[self.locationManager requestWhenInUseAuthorization]

[self.locationManager requestAlwaysAuthorization]

【讨论】:

    【解决方案2】:

    在 iOS 7 中我也遇到了同样的情况。我认为这是由于最后一个位置被缓存了;如果当前位置与上一个位置相比没有显着变化,则永远不会调用委托。

    您可以使用CLLocationManager 的“位置”属性访问最后缓存的位置:

    @property (readonly, nonatomic, copy) CLLocation *location ;
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-25
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 2011-07-19
      • 2017-09-15
      相关资源
      最近更新 更多