startMonitoringForRegion从不调用didEnterRegion / didExitRegion [英] startMonitoringForRegion never calls didEnterRegion/didExitRegion

查看:519
本文介绍了startMonitoringForRegion从不调用didEnterRegion / didExitRegion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试让iPhone4监控区域并通过调用didEnterRegion或didExitRegion通知我。我无法让它发挥作用。我正在阅读这里所有相关的enries,加上网上的几篇文章...... iOS只是不调用我的CLLocationManagerDelegate方法。
我做了什么:

I try to get the iPhone4 to monitor regions and notify me by call didEnterRegion or didExitRegion. I can't get it to work. I was reading probably all related enries here, plus a couple more articles on the web....iOS just don't call my CLLocationManagerDelegate methods. What did I do:

我有一个简单的AppDelegate,它也实现了didEnterRegion和didExitRegion的CLLocationManagerDelegate方法。在这些方法中,我只需使用UILocalNotification来报告事件。在ViewController中,我创建了一个区域(当前位置),其宽度为1000米。

I have a simple AppDelegate which implements also the CLLocationManagerDelegate methods for didEnterRegion and didExitRegion. Within these methods I simply use a UILocalNotification to report the event. From a ViewController I create a Region (the current Location) with aRadius of 1000meters.

推荐答案

以下是要检查的内容:


  1. 在开始监控代码中的区域之前,请调用 [CLLocationManager regionMonitoringAvailable] [CLLocationManager regionMonitoringEnabled] 以确保该服务在用户手机上可用并启用。

  1. Before you start monitoring regions in your code, call [CLLocationManager regionMonitoringAvailable] and [CLLocationManager regionMonitoringEnabled] to make sure the service is available and enabled on the user's phone.

确保将位置管理器的委托属性设置为您已实现的对象 locationManager:didEnterRegion:和/或 locationManager:didExitRegion:

Make sure you have the location manager's delegate property set to the object where you've implemented locationManager:didEnterRegion: and/or locationManager:didExitRegion:.

确保这些方法中没有任何拼写错误签名。小的大小写错误会导致这些消息的传递失败。将这些复制/粘贴到您的代码中并确保它们匹配:

Make sure you don't have any typos in those method signatures. A small capitalization error would cause delivery of these messages to fail. Copy/paste these into your code and make sure they match:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{ /* Handle enter */ }

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{ /* Handle exit */ }


  • 确保您的委托也实现 locationManager :monitoringDidFailForRegion:withError:,因为它可能会告诉你服务失败的原因。

  • Make sure your delegate also implements locationManager:monitoringDidFailForRegion:withError:, as it may tell you why the service is failing.

    - (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
    {
        NSLog(@"Region monitoring failed with error: %@", [error localizedDescription]);
    }
    


  • 可能发生此类监控失败的一个原因是核心位置对应用程序允许监视的区域数量施加限制。在实践中,这个限制似乎是每个应用程序大约十个区域。因此,请确保使用 stopMonitoringForRegion:删除不需要的区域,并按照Apple的位置感知编程指南

  • One reason a monitoring failure like this may occur is that Core Location imposes a limit on the number of regions an app is allowed to monitor. In practice this limit seems to be about ten regions per app. So make sure you remove regions you don't need using stopMonitoringForRegion:, and monitor only those regions nearest the user as recommended by Apple's Location Awareness Programming Guide:


    在将区域集指定为
    monitor时,您应始终谨慎。区域是共享系统资源,系统范围内可用的
    区域总数是有限的。因此,Core
    位置限制了单个应用程序可能同时监控
    的区域数量。要解决这些限制,您需要考虑仅在用户的直接
    附近注册那些区域。随着用户位置的变化,您可以删除
    现在更远的区域,并添加用户路径上的区域。如果
    你试图注册一个区域并且空间不可用,
    位置管理器调用
    locationManager:monitoringDidFailForRegion:withError:方法带有 kCLErrorRegionMonitoringFailure 错误代码的
    代表。

    You should always be judicious when specifying the set of regions to monitor. Regions are a shared system resource and the total number of regions available systemwide is limited. For this reason, Core Location limits the number of regions that may be simultaneously monitored by a single application. To work around these limits, you should consider registering only those regions in the user’s immediate vicinity. As the user’s location changes, you can remove regions that are now farther way and add regions coming up on the user’s path. If you attempt to register a region and space is unavailable, the location manager calls the locationManager:monitoringDidFailForRegion:withError: method of its delegate with the kCLErrorRegionMonitoringFailure error code.


  • 希望很明显,但请确保在设置代理后调用 startMonitoringForRegion:desiredAccuracy:

    初始化使用 initCircularRegionWithCenter:radius:identifier:监视的 CLRegion 对象时,请确保使用每个区域的唯一标识符。

    When you initialize the CLRegion object that you're monitoring using initCircularRegionWithCenter:radius:identifier:, make sure you use a unique identifier for each region.

    如果您的 locationManager:didEnterRegion: locationManager:didExitRegion:当应用程序处于活动状态时,方法会被正确调用,但是当操作系统在应用程序被杀后重新启动应用程序时,则不会,但是您可能无法正确初始化您的位置管理呃并在那种情况下设置其代表。如果您在应用程序未运行时跨越已注册的区域边界,操作系统将在后台启动您的应用程序,您可以使用 if([launchOptions objectForKey:@UIApplicationLaunchOptionsLocationKey]]){ 应用程序中的:您的应用程序委托的didFinishLaunchingWithOptions:方法。您的应用程序在后台启动时可能不会加载任何视图,因此您需要确保应用程序:didFinishLaunchingWithOptions:调用实例化您的位置管理器的一些代码路径在这种情况下对象并设置其委托。一旦您的位置经理的委托财产被设置,任何待处理的区域监控事件将被交付。

    If your locationManager:didEnterRegion: and locationManager:didExitRegion: methods are getting called properly when the app is active, but not when the OS relaunches your app in the background after it's been killed, then you may not be properly initializing your location manager and setting its delegate in that case. If you cross a registered region boundary when the app isn't running, the OS will launch your app in the background, which you can detect using if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]]) {} in the application:didFinishLaunchingWithOptions: method of your app delegate. Your app probably won't load any views when it's launched in the background like this, so you need to make sure application:didFinishLaunchingWithOptions: invokes some code path that instantiates your location manager object and sets its delegate in this case. As soon as your location manager's delegate property gets set, any pending region monitoring events will be delivered.

    这篇关于startMonitoringForRegion从不调用didEnterRegion / didExitRegion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆