xcode 从未调用过 LocationManager 的 didEnterRegion
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11592406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
didEnterRegion of LocationManager not called ever
提问by prashant solanki
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. I test this in my iPhone but it doesn't work. Please tell me what I am missing. My source code is:
我厌倦了发现这个问题,但无法理解为什么会发生这种情况,即位置管理器的委托方法didEnterRegion
或didExitRegion
从未被调用过。我在我的 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]; }
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
. I have also implemented the didFailWithError
but it does not do the thing. 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.
有人能弄清楚是什么问题吗??我搜索了许多演示和源代码,但大多数都以 结尾didUpdateToLocation
,在我的例子中被调用,但我对didEnter
和有问题didExit
。我也实现了,didFailWithError
但它没有做这件事。请给出为什么不调用这两种方法的任何线索。或者给我任何调用这些方法的链接,我没有找到这些方法的示例/来源。任何帮助将不胜感激。
回答by Nik Burns
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 应该可以正常触发。
回答by Muhammad Ibrahim
In viewDidLoad
, add the following code
在viewDidLoad
,添加以下代码
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.
我希望这能解决你的问题。
回答by Dave Wilson
I was able to get didEnterRegion and didExitRegion only after I enabled Background App Refresh in general settings.
只有在常规设置中启用后台应用程序刷新后,我才能获得 didEnterRegion 和 didExitRegion。
回答by Karin
Have you tried didDetermineState
as well? 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.
你也试过didDetermineState
吗?didEnterRegion
并且didExitRegion
仅在进入/退出时被调用,但据我所知,如果您在该地区并且实际上并未移动,didDetermineState
则会告诉您您目前是否在该地区。