4

我厌倦了找到这个问题,但无法理解为什么会发生这种情况,即位置管理器的委托方法didEnterRegiondidExitRegion从未被调用过。我在我的 iPhone 上进行了测试,但它不起作用。请告诉我我错过了什么。我的源代码是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // 应用程序启动后自定义的覆盖点。
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    manager = [[CLLocationManager alloc] init];
    manager.delegate = 自我;
    距离 = 100;
    坐标 = CLLocationCoordinate2DMake(24.004686, 74.554088);
    region=[[CLRegion alloc] initCircularRegionWithCenter:coord radius:dist identifier:@"emanuele"];
    [经理 startMonitoringForRegion:region desiredAccuracy:10];
    返回是;
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
    NSLog(@"输入区域。");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"知道了..." message:@"您已输入位置。" 委托:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [警报显示];
// [警报发布];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"didExitRegion");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"知道了..." message:@"您已退出该位置。" 委托:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [警报显示];
// [警报发布];
}

任何人都可以弄清楚有什么问题吗?我搜索了许多演示和源代码,但大多数都didUpdateToLocation以. 我也实施了,但它没有做的事情。请提供任何线索,为什么不调用这两种方法。或给我任何调用这些方法的链接,我没有找到这些方法的示例/来源。任何帮助将不胜感激。didEnterdidExitdidFailWithError

4

4 回答 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 应该可以正常触发。

于 2012-07-21T13:14:00.413 回答
0

viewDidLoad,添加以下代码

self.beaconRegion.notifyOnEntry=YES;
self.beaconRegion.notifyOnExit=YES;
self.beaconRegion.notifyEntryStateOnDisplay=YES;
[self.locationManager startMonitoringForRegion:self.beaconRegion];
[self.locationManager requestStateForRegion:self.beaconRegion];

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

于 2013-12-24T10:31:27.540 回答
0

只有在常规设置中启用后台应用程序刷新后,我才能获得 didEnterRegion 和 didExitRegion。

于 2014-06-29T22:49:08.617 回答
0

你也试过didDetermineState吗?didEnterRegion并且didExitRegion仅在进入/退出时被调用,但据我所知,如果您在该地区并且实际上没有移动,didDetermineState会告诉您您当前是否在该地区。

于 2020-03-11T10:43:58.910 回答