应用关闭时的startMonitoringForRegion [英] startMonitoringForRegion when App close
问题描述
我正在使用 startMonitoringForRegion
来监视特定区域。
当应用程序未运行时,我通过比较 [launchOptions objectForKey:@ UIApplicationLaunchOptionsLocationKey]
在用户输入/离开特定位置时成功生成了本地通知Appdelegate。
I am using startMonitoringForRegion
to monitor specific region.
When App is not running I have successfully generated a local notification when a user enters/leaves a particular location by comparing [launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]
in AppDelegate.
但是现在我不明白如何知道用户进入某个位置或离开某个位置。
我无法找到一种检查响应的方法。我可以在警报正文中显示响应吗?
我在互联网上搜索,但是找不到任何在AppDelegate中编写了一些代码的教程。
But now I am not able to understand how can I know that a user enters in a location or leaves a location. I am not able to find a way to check the response. Can I show the response in the alert body? I searched in internet but unable to find any tutorial that have written some code in AppDelegate.
推荐答案
位置管理器委托方法
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region{
if (state == CLRegionStateInside) {
// We entered a region
NSLog(@"Inside region");
if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground){
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Region Detected";
notification.soundName = @"Default";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
} else if (state == CLRegionStateOutside) {
// We are outside region
NSLog(@"Outside region");
}
}
在评价中
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"PerformAction" object:nil userInfo:nil];
}
在您的ViewController中viewDidLoad方法:
IN your ViewController viewDidLoad Method :
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(performAction) name:@"PerformAction" object:nil];
在同一viewController中添加以下方法并执行ur操作
In your same viewController add the following method and perform ur action
-(void)performAction
{
// perform your action
}
这篇关于应用关闭时的startMonitoringForRegion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!