简体   繁体   English

从未调用过 LocationManager 的 didEnterRegion

[英]didEnterRegion of LocationManager not called ever

I am tired of finding the problem with this but was unable to get why this happens that the Location Manager's delegate method didEnterRegion or didExitRegion is not called ever.我厌倦了发现这个问题,但无法理解为什么会发生这种情况,因为位置管理器的委托方法didEnterRegiondidExitRegion被调用过。 I test this in my iPhone but it doesn't work.我在我的 iPhone 上对此进行了测试,但它不起作用。 Please tell me what I am missing.请告诉我我错过了什么。 My source code is:我的源代码是:

- (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];
}

Can anybody figure out what's wrong with??任何人都可以弄清楚出了什么问题吗? I have searched many demos and source codes but most of them end with didUpdateToLocation , which is called in my case, but I have problem with didEnter and didExit .我搜索了很多演示和源代码,但其中大部分都以didUpdateToLocation ,在我的案例中调用了它,但我对didEnterdidExit有疑问。 I have also implemented the didFailWithError but it does not do the thing.我也实现了didFailWithError但它没有做这件事。 Please give any clue why these two methods are not called.请给出为什么不调用这两种方法的任何线索。 or give me any link that calls these methods, I found no examples/source for these methods.或者给我任何调用这些方法的链接,我没有找到这些方法的示例/源代码。 Any help will be appreciated.任何帮助将不胜感激。

is your header file declared as a CLLocationManagerDelegate?您的头文件是否声明为 CLLocationManagerDelegate?

once you setup the region for monitoring you should test by removing that element of the code and then check for the UIApplicationLaunchOptionsLocationKey key when launching like this:一旦你设置了监控区域,你应该通过删除该代码元素进行测试,然后在启动时检查 UIApplicationLaunchOptionsLocationKey 键,如下所示:

 if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]){

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


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

    }

The location manager delegate will then be called and didEnter and didExit should fire ok.然后将调用位置管理器委托,并且 didEnter 和 didExit 应该正常触发。

In viewDidLoad , add the following codeviewDidLoad中,添加以下代码

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

I hope this will solve your problem.我希望这会解决你的问题。

I was able to get didEnterRegion and didExitRegion only after I enabled Background App Refresh in general settings.只有在常规设置中启用后台应用程序刷新后,我才能获得 didEnterRegion 和 didExitRegion。

Have you tried didDetermineState as well?您是否也尝试过didDetermineState didEnterRegion and didExitRegion are only called on enter/exit, but from what I understand if you're in the region and not actually moving, didDetermineState will tell if you if you are currently in the region. didEnterRegiondidExitRegion仅在进入/退出时调用,但根据我的理解,如果您在该地区并且实际上没有移动, didDetermineState会告诉您您当前是否在该地区。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 方法LocationManager:didEnterRegion和LocationManager:didExitRegion被连续调用 - Methods LocationManager:didEnterRegion and LocationManager:didExitRegion is getting called continuously locationManager:didEnterRegion:触发时未更改位置 - locationManager: didEnterRegion: triggered without location change 为什么 didEnterRegion 调用了两次? - why the didEnterRegion called twice? 重复调用locationManager didFailWithError - locationManager didFailWithError Repeatedly Called 监视CLCirculerregion didStartMonitoring已调用,但didEnterRegion或didExitRegion从未调用 - Monitoring CLCirculerregion didStartMonitoring called,but didEnterRegion or didExitRegion never called 从未调用过CLLocationManager回调(locationManager:didUpdateToLocation:fromLocation和locationManager:didFailWithError :) - CLLocationManager callbacks (locationManager:didUpdateToLocation:fromLocation and locationManager:didFailWithError:) never called 对于某些iPhone,仅在打开屏幕后才调用信标didenterregion - For some iPhones the beacon didenterregion gets only called after screen is turned on locationManager:didExitRegion:屏幕关闭时不调用 - locationManager:didExitRegion: not called when screen is off locationManager:didUpdateLocations:总是被多次调用 - locationManager:didUpdateLocations: always be called several times 没有网络时不调用CLLocationManagerDelegate -locationManager(_:startMonitoring :) - CLLocationManagerDelegate -locationManager(_:startMonitoring:) not called when there is no network
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM