没有调用LocationManager的didEnterRegion

时间:2012-07-21 13:01:44

标签: iphone ios xcode ipad cllocationmanager

我已经厌倦了找到这个问题,但却无法理解为什么会发生这种情况,即位置管理器的委托方法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,但它没有做到这一点。请提供任何线索,为什么不调用这两种方法。或者给我任何调用这些方法的链接,我没有找到这些方法的示例/来源。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分: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应该正常启动。

答案 1 :(得分:0)

viewDidLoad中,添加以下代码

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

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

答案 2 :(得分:0)

我在一般设置中启用后台应用刷新后才能获得didEnterRegion和didExitRegion。

答案 3 :(得分:0)

您是否也尝试过didDetermineStatedidEnterRegiondidExitRegion仅在进入/退出时被调用,但据我了解您是否在该地区并且实际上并未移动,didDetermineState会告诉您是否当前在该地区。