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

How to suspend beacon's notification while in range?

我在 iOS 开发方面是个新手,几周后我就开始使用 iBeacon。我目前正在开发一个应用程序,可以在 he/she 进入信标范围(例如商店部分)时向用户发送优惠券。这张优惠券只能偶尔发送一次,但即使在发送完成后用户也很可能留在信标的范围内,所以我需要应用程序暂停 "listening" 到特定的信标固定数量时间,比方说 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;
    }
}

我怎样才能添加一些计时器或相关的东西让应用程序暂时忽略信标?

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

以下示例展示了如何在重复信标事件上添加 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

}