Я знаю, что основные цели
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region
должен работать, пока приложение находится на переднем плане.
Находясь в фоновом режиме,
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
используется для обнаружения маяков, но не с таким количеством информации, как хотелось бы (второстепенные и основные идентификаторы из CLBeacons для предоставления контекстной информации).
Я знаю, что методы делегата из CLLocationManager позволяют ненадолго запустить код в фоновом режиме.
Можно ли сделать что-то вроде этого: - начать кратко ранжировать маяки в фоновом режиме при входе в регион - вызвать веб-службу в фоновом режиме в соответствии с младшими/основными идентификаторами - отправить 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
}
}