ios - 30 秒 - 1 分钟后检查用户是否仍在 CLRegion 内

标签 ios swift cllocationmanager

我目前在用户进入区域时向他们发送本地通知。但是,我不想立即通知他们,因为有时他们会开车经过这些区域并且不想一直收到有关这些区域的通知。

因此,每当用户进入该区域时,都会调用 didEnterRegion。我想等待 30 秒(或可配置的时间),然后检查用户是否仍在该区域(我已经知道该怎么做),如果用户仍在该区域,则通知用户。

Android 有一种使用 dwell 属性的方法。

我知道我可以使用 dispatch_after(),但是当手机处于休眠状态(或应用程序挂起)时我无法获得主线程。

根据 Apple 的设计,当进入某个区域时,手机会自动唤醒。发生这种情况时,我想等待 30 秒,然后再次检查用户是否仍在原地。这是一个目前不起作用的代码片段:

// triggered when user enters monitored region.
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
    // Wait 60 seconds and if user is still within region, try to send notification 
    delay(60){
        self.locationManager.requestStateForRegion(region)
    }
}

func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) {
    // only notify user if user is still within this region.
    if state == CLRegionState.Inside{
        if region is CLCircularRegion {
            handleRegionEvent(region)
        }
    }
}

最佳答案

有一种方法可以做到这一点: 当应用程序收到用户进入某个区域的通知时,它应该在 30 秒后安排本地通知。您需要在此通知中设置标识符。 现在,如果没有任何反应,该通知将发送给用户。

什么情况会阻止这种情况发生? 用户离开该区域。

因此,当应用收到用户已离开某个区域的通知时,它应该查找具有某些已知标识符的预定通知,并在它不再相关时将其删除。

关于ios - 30 秒 - 1 分钟后检查用户是否仍在 CLRegion 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37147900/

相关文章:

swift - 在 Swift 中加载 UICollectionView 时的加载器/旋转器

ios - 启用后禁用 allowsBackgroundLocationUpdates (CLLocationManager) 不起作用

ios - 尝试在 MKMapView 中查找用户位置的问题

ios - 在iPhone上存储应用程序数据的最佳方法是什么?

ios - Bluetooth Low Energy - 重复更新特征值

ios - 在 View 上点击时如何隐藏/显示我的导航栏(不是导航 Controller )?

ios - 当我滚动 TableView 时,它删除了选定的项目 - Objective-c

swift - 在 iOS 11 中向 UINavigationBar 添加项目后面的 subview

swift - 在哪种情况下足以使用隐式展开的可选值?

swift - 为什么是locationManager(:didUpdateLocations) not called