我正在我的应用程序中使用区域监控,当我在应用程序启动时在城市中行走时,它的工作非常顺利。
我的问题是在我的应用终止时收到这些位置更改通知。我的应用程序因位置变化而被唤醒时崩溃,当我尝试在应用程序中设置rootViewController时崩溃:didFinishLaunchingWithOptions:method。
当我的应用程序因位置变化而在后台启动时,我应该以不同方式实现此方法吗?
当我的应用程序在进入/激发区域时被唤醒时,我在后台执行任务需要多长时间?
答案 0 :(得分:0)
要回答您的第二个问题,iOS / Apple表示您有大约10秒的时间来完成必要的工作。
第一个问题:
在你的app delegate.h中,添加:CLLocationManagerDelegate
在application:didFinishLaunchingWithOptions:
方法中添加:
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
并补充说:
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
[notification setSoundName:UILocalNotificationDefaultSoundName];
[notification setApplicationIconBadgeNumber:0];
if(state == CLRegionStateInside) {
[notification setAlertBody:@"CLRegionStateInside"];
}
else if(state == CLRegionStateOutside) {
[notification setAlertBody:@"CLRegionStateOutside"];
}
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
然后,如果您有区域代码(可能在某个视图控制器中某处),请添加:[yourRegion setNotifyEntryStateOnDisplay:YES];
:
[locationManager startMonitoringForRegion:yourRegion];
这就是全部!