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 上工作缺少什么的任何想法?

谢谢!

4

1 回答 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/

于 2014-01-18T00:21:14.373 回答