iOS 7 didEnterRegion根本没有被调用

San*_*iya 6 cllocationmanager clregion ios7 clcircleregion

我使用以下代码来监控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]];
Run Code Online (Sandbox Code Playgroud)

//在AppDelegate.m中

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

我还在plist文件中将所需的后台模式设置为"应用寄存器以进行位置更新".

有关此功能缺少什么的想法可以在iOS7上运行吗?

谢谢!

小智 0

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

//LocationManagerClass.h

@interface LocationManagerClass : NSObject

      {... other stuff in the interface file}

- (void)beginMonitoringRegion:(CLRegion *)region;

@end
Run Code Online (Sandbox Code Playgroud)

然后在

//LocationManagerClass.m

@interface LocationManagerClass () <CLLocationManagerDelegate>
@end

@implementation LocationManagerClass

     {... other important stuff like locationManager:didEnterRegion:}

- (void)beginMonitoringRegion:(CLRegion *)region
{
    [[CLLocationManager sharedManager] startMonitoringForRegion:region];
}

@end
Run Code Online (Sandbox Code Playgroud)

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

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