startMonitoringForRegion 从不调用 didEnterRegion/didExitRegion

发布于 2024-10-02 13:58:18 字数 322 浏览 5 评论 0原文

我尝试让iPhone4监视区域并通过调用didEnterRegion或didExitRegion通知我。我无法让它工作。我在这里阅读了可能所有相关的条目,以及网上的几篇文章....iOS 只是不调用我的 CLLocationManagerDelegate 方法。 我做了什么:

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

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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一袭水袖舞倾城 2024-10-09 13:58:18

以下是一些需要检查的事项:

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

  2. 确保将位置管理器的 delegate 属性设置为已实现 locationManager:didEnterRegion: 和/或 locationManager:didExitRegion:< 的对象/code>.

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

    - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
    { /* 处理输入 */ }
    
    - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
    { /* 处理退出 */ }
    
  4. 确保您的委托还实现 locationManager:monitoringDidFailForRegion:withError:,因为它可能会告诉您服务失败的原因。

    - (void)locationManager:(CLLocationManager *)managingDidFailForRegion:(CLRegion *)region withError:(NSError *)error
    {
        NSLog(@"区域监控失败,错误:%@", [error localizedDescription]);
    }
    
  5. 可能发生此类监视失败的原因之一是 Core Location 对允许应用程序监视的区域数量施加了限制。实际上,这个限制似乎是每个应用程序大约十个区域。因此,请确保使用 stopMonitoringForRegion: 删除不需要的区域,并按照 Apple 位置感知编程指南

    <块引用>

    在指定区域集时应始终保持明智
    监视器。区域是共享的系统资源,区域的总数
    全系统可用的区域是有限的。为此,核心
    位置限制了可以同时进行的区域数量
    由单个应用程序监控。要解决这些限制,您
    应该考虑只注册用户直接使用的那些区域
    附近。当用户的位置发生变化时,您可以删除那些区域
    现在距离更远,并添加用户路径上出现的区域。如果
    您尝试注册一个区域但空间不可用,则
    位置经理致电
    locationManager:monitoringDidFailForRegion:withError: 方法
    带有 kCLErrorRegionMonitoringFailure 错误代码的委托。

  6. 希望很明显,但请确保您在设置委托后调用 startMonitoringForRegion:desiredAccuracy:

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

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

Here are some things to check:

  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.

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

  3. 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 */ }
    
  4. 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]);
    }
    
  5. 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:

    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.

  6. Hopefully obvious, but make sure you're calling startMonitoringForRegion:desiredAccuracy: after setting your delegate.

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

  8. 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.

全部不再 2024-10-09 13:58:18

iOS 5.1 Simulator 存在一个已知错误,即不会调用区域监视委托方法。我的经验是它会调用第一个触发的方法(无论是输入还是输出),但之后不会再调用。我已经向苹果公司提交了关于这一事实的雷达报告,但在当前的迭代中大部分被忽略了。他们确实回应询问它是否可以在 iOS 6 beta 中运行,确实如此。我根本不指望他们会在 5.1 版本中修复它。

您可以下载仍然可以使用的 5.0 模拟器,或者下载 iOS 6 beta(如果您是付费开发者)并在那里测试您的位置信息。除此之外,我们需要查看一些代码来显示所有设置是否正确。我猜如果区域监控图标出现,则说明您做得正确并且只是模拟器错误的受害者。

There is a known bug with iOS 5.1 Simulator where the region monitoring delegate methods are not called. My experience with it is it will call the first method triggered (either in or out) but won't call any more after that. I have filed radars with Apple to this fact, but have been mostly ignored for the current iteration. They did respond asking if it was working in the iOS 6 beta, which it is. I don't expect them to fix it for 5.1 at all.

You can download the 5.0 simulator which still works, or download the iOS 6 beta (if you are a paid dev) and test your location stuff there. Outside that, we would need to see some code showing if everything is set up correctly. I'm guessing if the region monitoring icon shows up, you are doing it right and are just victim of the simulator bug.

我们的影子 2024-10-09 13:58:18

同样在这里。我也在尝试获取 didEnterRegion 并通知我。我看到的是该方法永远不会被调用。所以我有一个自定义签入(newLocation,oldLocation 函数)

分配一个距离并且:

{
distance = [newLocation distanceFromLocation:(your location)];
if(distance < 300){
[appDelegate Notify:@"Entering "];
}

重大位置更改很糟糕!

Same here. I am trying too to get didEnterRegion and notify me. What I see is that the method never gets called. So I am having a custom check in (newLocation, oldLocation function)

allocate a distance and :

{
distance = [newLocation distanceFromLocation:(your location)];
if(distance < 300){
[appDelegate Notify:@"Entering "];
}

significantlocationchanges sucks!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文