【问题标题】:iOS 7 didEnterRegion not getting called at alliOS 7 didEnterRegion 根本没有被调用
【发布时间】:2013-10-18 11:24:49
【问题描述】:

我正在使用以下代码来监控我的 iOS 应用程序中的区域。当我在 iOS6 上构建应用程序时,它可以完美运行。当我在 iOS7 上构建它时,没有触发 didEnterRegion。

// 在 iOS 中创建和注册一个区域

CLLocationCoordinate2D venueCenter = CLLocationCoordinate2DMake([favoriteVenue.venueLat      doubleValue], [favoriteVenue.venueLng doubleValue]);
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:venueCenter radius:REGION_RADIUS identifier:favoriteVenue.venueId];

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate.locationManager startMonitoringForRegion:[self regionForVenue:favoriteVenue]];

//在AppDelegate.m中

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"Entered region: %@", region.identifier);
}

我还在我的 plist 文件中将所需的后台模式设置为“应用程序注册以进行位置更新”。

对于在 iOS7 上运行此功能缺少什么有什么想法吗?

谢谢!

【问题讨论】:

    标签: ios7 cllocationmanager clregion clcircleregion


    【解决方案1】:

    应该适用于 iOS 6 和 7 的方法是在您的类中创建一个符合 CLLocationManagerDelegate 协议的公共方法,告诉自己开始监视该区域。例如:

    //LocationManagerClass.h
    
    @interface LocationManagerClass : NSObject
    
          {... other stuff in the interface file}
    
    - (void)beginMonitoringRegion:(CLRegion *)region;
    
    @end
    

    然后在

    //LocationManagerClass.m
    
    @interface LocationManagerClass () <CLLocationManagerDelegate>
    @end
    
    @implementation LocationManagerClass
    
         {... other important stuff like locationManager:didEnterRegion:}
    
    - (void)beginMonitoringRegion:(CLRegion *)region
    {
        [[CLLocationManager sharedManager] startMonitoringForRegion:region];
    }
    
    @end
    

    所以在你的情况下你会打电话给[appDelegate beginMonitoringRegion:region];

    附带说明,我建议不要将您的位置管理代码放在应用程序委托中。尽管从技术上讲它会起作用,但对于这样的事情,它通常不是一个好的设计模式。相反,在上面的示例中,我会尝试将它放在它自己的位置管理器类中,这可能是一个单例。这篇博文提供了一些很好的支持,说明为什么不在应用程序委托中放置大量内容:http://www.hollance.com/2012/02/dont-abuse-the-app-delegate/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2016-03-16
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      • 2023-03-21
      • 2017-11-12
      • 1970-01-01
      相关资源
      最近更新 更多