gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:43:25 24 4
gpt4 key购买 nike

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

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

最佳答案

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

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

}

关于ios - 如何在范围内暂停信标的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31209276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com