I know that the main purposes of the
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region
在应用程序前台工作。
在后台运行时,
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
用于检测信标,但提供的信息不如所需(从CLBeacons获取的次要和主要ID可提供上下文信息)。
我知道CLLocationManager的委托方法允许在后台运行短暂的代码。
是否可能做到这样: - 进入区域时短暂地在后台范围内的信标 - 根据次要/主要ID在后台调用Web服务 - 调度使用Web服务返回结果配置的UILocalNotification
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
{
if (![region isKindOfClass:[CLBeaconRegion class]]) return;
[locationManager startRangingBeaconsInRegion:(CLBeaconRegion*)region];
}
然后:
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region
{
if (beacons.count == 0) return;
CLBeacon *foundBeacon = [sortedBeacons firstObject];
// DO STUFF IN BACKGROUND
if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive){
//Call for a webservice according to minor /major
//Dispatch contextual notification
// ^success(...)
UILocalNotification * theNotification = [[UILocalNotification alloc] init];
theNotification.alertBody = [NSString stringWithFormat:@""];
theNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
[[UIApplication sharedApplication] scheduleLocalNotification:theNotification];
}
else{ //DO STUFF IN FOREGROUND
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"proximity"
ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"accuracy"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor,sortDescriptor2];
NSArray *sortedBeacons = [beacons sortedArrayUsingDescriptors:sortDescriptors];
//determine which beacon will be used to trigger foreground event
}
}