iOS 7的didEnterRegion完全没有被调用

6

我正在使用以下代码监控我的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上使此功能正常工作还需要什么呢?
谢谢!
1个回答

0

适用于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/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接