位置管理器的didEnterRegion从未被调用过

4
我已经非常疲倦地找到了这个问题,但是仍然无法弄清楚为什么始终不会调用位置管理器的委托方法didEnterRegiondidExitRegion。我在我的iPhone上测试了这个问题,但它不起作用。请告诉我我错过了什么。我的源代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
manager = [[CLLocationManager alloc] init]; manager.delegate = self; dist = 100; coord = CLLocationCoordinate2DMake(24.004686, 74.554088); region=[[CLRegion alloc] initCircularRegionWithCenter:coord radius:dist identifier:@"emanuele"]; [manager startMonitoringForRegion:region desiredAccuracy:10]; return YES; }
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{ NSLog(@"Enter Region."); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got it..." message:@"You have Entered the Location." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; // [alert release]; } - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { NSLog(@"didExitRegion"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got it..." message:@"You have Exited the Location." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; // [alert release]; }
有人能弄清楚问题出在哪里吗? 我搜索了许多演示和源代码,但其中大多数都以didUpdateToLocation结尾,在我的情况下调用了该方法,但是我遇到了didEnterdidExit的问题。我还实现了didFailWithError,但它没有起作用。请给出任何线索,说明为什么这些方法不被调用。或者给我任何一个会调用这些方法的链接,我找不到这些方法的示例/源代码。任何帮助将不胜感激。

1
你尝试过实现locationManager:monitoringDidFailForRegion:withError:来查看是否出现了某种错误吗? - Pfitz
@HeikoG:不,我没有使用ARC。 - prashant solanki
@Pfitz:是的,我已经实现了那个方法,但它没有报错(控制流从未进入该方法)。 - prashant solanki
你是如何声明你的区域属性呢?使用 init 创建实例,然后再添加。CLRegion *newRegion = [[CLRegion alloc] initCircularRegionWithCenter....... - Nik Burns
@Nik Burns: coord = CLLocationCoordinate2DMake(28.004686, 78.554088); region=[[CLRegion alloc] initCircularRegionWithCenter:coord radius:dist identifier:@"emanuele"]; - prashant solanki
可能这会对你有所帮助:https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/DeprecationAppendix/AppendixADeprecatedAPI.html,简而言之,在iOS7+中`regionMonitoringAvailable`已被弃用。 - holex
4个回答

1
你的头文件是否声明为CLLocationManagerDelegate?
一旦你设置了监控区域,你应该通过删除代码中的那个元素来进行测试,然后在启动时检查UIApplicationLaunchOptionsLocationKey键,像这样:
 if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]){

        NSLog(@"UIApplicationLaunchOptionsLocationKey - fired");
        if(locman == nil){
            locman = [[CLLocationManager alloc]init];


        locman.delegate = self;
        locman.distanceFilter = kCLLocationAccuracyBest;
        }

    }

位置管理器委托将被调用,然后应该触发didEnter和didExit。

最好的做法是同时使用kCLLocationAccuracyHundredMeters或等效的精度。 - Nik Burns
非常感谢您的回答。现在问题是控制器从未进入if语句……有任何想法为什么会发生这种情况吗?我已经在info.plist文件中设置了后台模式为voip、位置和音频。我还设置了委托并实现了CLLocationManagerDelegate协议到appDelegate。问题似乎太傻了,但它已经变得严重起来。 - prashant solanki
您不需要在info.plist中添加VOIP位置或音频条目来进行didEnterRegion回调。只要应用程序监视正确的区域,它们就可以工作。您是否使用正确的纬度/经度在模拟器上进行了测试?如果您已经成功地注册了要监视的区域,那么在完全关闭应用程序的情况下,当您更改模拟器位置时,您将会收到回调。 - Nik Burns
只是为了澄清:您尝试在应用程序正在运行而不是后台运行时收到通知。 - Pfitz
如果您的应用程序在后台运行,则不会调用@prashantsolanki委托。 - Pfitz
显示剩余2条评论

0
viewDidLoad 中,添加以下代码。
self.beaconRegion.notifyOnEntry=YES;
self.beaconRegion.notifyOnExit=YES;
self.beaconRegion.notifyEntryStateOnDisplay=YES;
[self.locationManager startMonitoringForRegion:self.beaconRegion];
[self.locationManager requestStateForRegion:self.beaconRegion];

我希望这能解决你的问题。


0

只有在我在“通用设置”中启用了“后台应用程序更新”之后,我才能获得didEnterRegion和didExitRegion。


0
你尝试过使用didDetermineState吗?didEnterRegiondidExitRegion只在进入/退出时调用,但据我所知,如果你在区域内而且没有移动,didDetermineState会告诉你当前是否在该区域内。

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