2015-07-03 24 views
0

我在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; 
    } 
} 

我如何可以添加一些自拍或使應用程序忽略了,而標相關的東西嗎?

回答

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 

}