ios - 当 iBeacon 范围为 "Immediate"或 "near"时如何通知用户?

标签 ios objective-c ibeacon

用户只有在离信标足够近时才会收到通知,因为那时 didEnterRegion 无法正常工作。
我的代码是这样的:

if ([region isKindOfClass:[CLBeaconRegion class]] && ([beaconRegionInStringFormat isEqualToString:@"Immediate"] || [beaconRegionInStringFormat isEqualToString:@"Near"]))
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"Please open the application";
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

但通知将继续发送给用户。我怎样才能只收到一次通知?

最佳答案

1. 给你的 Controller 添加一个标志

界面:
@property (nonatomic) BOOL userNotified;
执行:

if (!self.userNotified && [region isKindOfClass:[CLBeaconRegion class]] && ([beaconRegionInStringFormat isEqualToString:@"Immediate"] || [beaconRegionInStringFormat isEqualToString:@"Near"]))
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"Please open the application";
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
    self.userNotified = YES;
}

2. 使用 GCD (Grand Central Dispatch)

使用此方法,每次应用启动时代码只会执行一次。
static dispatch_once_t onceToken;
dispatch_once (&onceToken, ^{
    if ([region isKindOfClass:[CLBeaconRegion class]] && ([beaconRegionInStringFormat isEqualToString:@"Immediate"] || [beaconRegionInStringFormat isEqualToString:@"Near"]))
    {
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.alertBody = @"Please open the application";
        [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
    }
});

关于ios - 当 iBeacon 范围为 "Immediate"或 "near"时如何通知用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34652357/

相关文章:

ios - 不同屏幕尺寸下的UITableViewCell

c# - 如何在 c# Xamarin 中实现 ios 委托(delegate)/数据源模式?

objective-c - UIView drawRect() : fill everything except polygon

macos - CBPeripheralManager startAdvertising 不适用于 OS X 优胜美地

ios - 具有 UIButton 名称的 For 循环

ios - 在自定义 UICollectionViewCell 中缩放图像

ios - 如何快速获取键值对元组数组?

objective-c - Objective-C 静态库中的内存管理

ios - Estimote Beacon 加速度计更新时间

ios - 如何防止Ibeacons中的clone,避免beacons之间的冲突?