【问题标题】:Can you stop regions from persisting between launches with CLLocationManager?您可以使用 CLLocationManager 阻止区域在启动之间持续存在吗?
【发布时间】:2014-09-28 17:07:53
【问题描述】:

有没有办法防止 CLLocationManager 在启动之间保持受监控的区域?每次启动应用程序时,我都需要添加一组新的受监控区域,而旧的不再有用。有没有办法阻止它们持续存在或在启动时清除所有旧的?

【问题讨论】:

    标签: ios core-location geofencing clregion clcircleregion


    【解决方案1】:

    当然可以清除当前监控的所有区域:

    +(void)clearRegionWatch
    {
        for(CLRegion *region in [[WGLocation shared].locationManager monitoredRegions]){
            [[WGLocation shared].locationManager stopMonitoringForRegion:region];
        }
    }
    

    如果您有要删除的特定标识符:

    +(void)clearRegionWatchForKey:(NSString *)key
    {
        for(CLRegion *region in [[WGLocation shared].locationManager monitoredRegions]){
            if([region.identifier isEqualToString:key]){
                [[WGLocation shared].locationManager stopMonitoringForRegion:region];
            }
        }
    }
    

    您可以将函数的内部结构复制到应用程序的适当位置。我已经从我的共享经理类中复制了它们。

    【讨论】:

    • 我发现这不可靠。有时它会停止监视所有区域,有时它只停止监视其中一些区域。
    • 有趣的是,在这种情况下,您是否考虑过重用相同的标识符?这将覆盖“不再有用”的区域
    【解决方案2】:

    SWIFT 4 中 你可以阻止所有区域被监控,就像

    let monitoredRegions = locationManager.monitoredRegions
    
    for region in monitoredRegions{
        locationManager.stopMonitoring(for: region)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多