如何在范围内暂停信标的通知?

时间:2015-07-03 14:34:59

标签: ios ibeacon

我是iOS开发中的新手,我开始使用iBeacon几周了。我目前正在开发一款应用,可以在用户进入信标范围(例如商店部分)时向用户提供优惠券。此优惠券必须偶尔交付一次,但即使在交付完成后,用户也很可能会留在信标的范围内,因此我需要该应用暂停"收听"对于那个特定的灯塔一段固定的时间,让我们说30分钟。

这是我对locationManager:didRangeBeacons:inRegion:

的实现
- (void)locationManager:(CLLocationManager *)manager
        didRangeBeacons:(NSArray *)beacons
               inRegion:(CLBeaconRegion *)region {
    if (foundBeacons.count == 0) {
        for (CLBeacon *filterBeacon in beacons) {
            // If a beacon is located near the device and its major and minor values are equal to some constants
            if (((filterBeacon.proximity == CLProximityImmediate) || (filterBeacon.proximity == CLProximityNear)) && ([filterBeacon.major isEqualToNumber:[NSNumber numberWithInt:MAJOR]]) && ([filterBeacon.minor isEqualToNumber:[NSNumber numberWithInt:MINOR]]))
                // Registers the beacon to the list of recognized beacons
                [foundBeacons addObject:filterBeacon];
        }
    }
    // Did some beacon get found?
    if (foundBeacons.count > 0) {
        // Takes first beacon of the list
        beacon = [foundBeacons firstObject];

        if (([beacon.major isEqualToNumber:[NSNumber numberWithInt:MAJOR]]) && ([beacon.minor isEqualToNumber:[NSNumber numberWithInt:MINOR]])) {
            // Plays beep sound
            AudioServicesPlaySystemSound(soundFileObject);

            if (self.delegate) {
                // Performs actions related to the beacon (i.e. delivers a coupon)
                [self.delegate didFoundBeacon:self];
            }
            self.locationManager = nil;
        }
        [foundBeacons removeObjectAtIndex:0];
        beacon = nil;
    }
}

如何添加一些计时器或相关内容以使应用程序暂时忽略信标?

1 个答案:

答案 0 :(得分:2)

一种常见的技术是保留一个数据结构,告诉您上次对信标采取操作的时间,然后如果自上次这样做以来没有经过足够的时间,则避免再次采取操作。

以下示例显示如何在重复信标事件上添加10分钟(600秒)过滤器。

// Declare these in your class
#define MINIMUM_ACTION_INTERVAL_SECONDS 600
NSMutableDictionary *_lastBeaconActionTimes;

...

// Initialize in your class constructor or initialize method
_lastBeaconActionTimes = [[NSMutableDictionary alloc] init];

...

// Add the following before you take action on the beacon

NSDate *now = [[NSDate alloc] init];
NSString *key = [NSString stringWithFormat:@"%@ %@ %@", [beacon.proximityUUID UUIDString], beacon.major, beacon.minor];
NSDate *lastBeaconActionTime = [_lastBeaconActionTimes objectForKey:key];
if (lastBeaconActionTime == Nil || [now timeIntervalSinceDate:lastBeaconActionTime] > MINIMUM_ACTION_INTERVAL_SECONDS) {
  [_lastBeaconActionTimes setObject:now forKey:key];

  // Add your code to take action here

}