【问题标题】:didEnterRegion of LocationManager not called ever从未调用过 LocationManager 的 didEnterRegion
【发布时间】:2012-07-21 13:01:43
【问题描述】:

我厌倦了找到这个问题,但无法理解为什么会发生这种情况,因为位置管理器的委托方法 didEnterRegiondidExitRegion 从未被调用过。我在我的 iPhone 上进行了测试,但它不起作用。请告诉我我错过了什么。我的源代码是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // 应用程序启动后自定义的覆盖点。 self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; manager = [[CLLocationManager alloc] init]; manager.delegate = 自我; 距离 = 100; 坐标 = CLLocationCoordinate2DMake(24.004686, 74.554088); region=[[CLRegion alloc] initCircularRegionWithCenter:coord radius:dist identifier:@"emanuele"]; [经理 startMonitoringForRegion:region desiredAccuracy:10]; 返回是; } - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{ NSLog(@"输入区域。"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"知道了..." message:@"您已输入位置。"委托:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [警报显示]; // [警报发布]; } - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { NSLog(@"didExitRegion"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"知道了..." message:@"您已退出该位置。"委托:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [警报显示]; // [警报发布]; }

任何人都可以弄清楚有什么问题吗? 我搜索了许多演示和源代码,但大多数都以didUpdateToLocation 结尾,在我的案例中称为didEnterdidExit。我也实现了didFailWithError,但它并没有做到这一点。请提供任何线索,为什么不调用这两种方法。或给我任何调用这些方法的链接,我没有找到这些方法的示例/来源。任何帮助将不胜感激。

【问题讨论】:

  • 您是否尝试过实现locationManager:monitoringDidFailForRegion:withError: 以查看是否出现某种错误?
  • @HeikoG:不,我没有使用 ARC。
  • @Pfitz:是的,我已经实现了那个方法,但是它没有给出任何错误(控件永远不会进入方法内部。)...
  • 您如何声明您的区域属性?初始化为新的,然后添加。 CLRegion *newRegion = [[CLRegion alloc] initCircularRegionWithCenter.......
  • @Nik Burns: coord = CLLocationCoordinate2DMake(28.004686, 78.554088); region=[[CLRegion alloc] initCircularRegionWithCenter:coord radius:dist identifier:@"emanuele"];

标签: iphone ios xcode ipad cllocationmanager


【解决方案1】:

您的头文件是否声明为 CLLocationManagerDelegate?

一旦您设置了监控区域,您应该通过删除该代码元素进行测试,然后在启动时检查 UIApplicationLaunchOptionsLocationKey 键,如下所示:

 if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]){

        NSLog(@"UIApplicationLaunchOptionsLocationKey - fired");
        if(locman == nil){
            locman = [[CLLocationManager alloc]init];


        locman.delegate = self;
        locman.distanceFilter = kCLLocationAccuracyBest;
        }

    }

然后将调用位置管理器委托,并且 didEnter 和 didExit 应该可以正常触发。

【讨论】:

  • 良好的做法是同时使用 kCLLocationAccuracyHundredMeters 或等效的精度。
  • 非常感谢您的回答。现在的问题是控件永远不会进入 if...知道为什么会发生这种情况吗?我在 info.plist 文件中将背景模式设置为 voip、位置和音频。我还为 appDelegate 设置了委托并实现了 CLLocationManagerDelegate 协议。这个问题看起来太傻了,但它已经变得严重了。
  • 对于 didEnterRegion 回调,您不需要 info.plist 中的 voip 位置或音频条目。只要应用程序监控正确的区域,它们就可以工作。您是否在模拟器上使用正确的纬度/经度进行了测试?如果您已成功注册区域进行监控,那么在应用程序完全关闭的情况下,您将在更改模拟器位置时收到回调。
  • 只是为了澄清:您试图在应用程序运行时而不是在后台时获得通知
  • @Pfitz:是的,我知道,我将 alertview 仅用于测试。我知道当应用程序在后台时它不会显示。但无论如何它都没有显示出来。
【解决方案2】:

只有在常规设置中启用后台应用刷新后,我才能获得 didEnterRegion 和 didExitRegion。

【讨论】:

    【解决方案3】:

    viewDidLoad中,添加如下代码

    self.beaconRegion.notifyOnEntry=YES;
    self.beaconRegion.notifyOnExit=YES;
    self.beaconRegion.notifyEntryStateOnDisplay=YES;
    [self.locationManager startMonitoringForRegion:self.beaconRegion];
    [self.locationManager requestStateForRegion:self.beaconRegion];
    

    希望这能解决你的问题。

    【讨论】:

      【解决方案4】:

      你也试过didDetermineState 吗? didEnterRegiondidExitRegion 仅在进入/退出时调用,但据我了解,如果您在该地区并且实际上没有移动,didDetermineState 会告诉您您目前是否在该地区。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-12
        • 2023-03-21
        • 2016-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多