设备重启后,iOS区域监控没有显着的位置更改

问题描述 投票:0回答:1

我正在尝试实现它需要检测设备是否在目标区域(地理围栏)内部的功能。如果它在区域内,它应该执行一些任务,无论它是在前景还是后台。

我能够找出大部分零件并实施。

然而,挑战在于用户在已经在区域内部时重启设备并且不进行任何显着的位置改变的情况。

在这种情况下,似乎iOS无法检测它是在内部还是外部。因此我的应用程序什么都不做,而不是执行任务。

那么有没有什么方法可以让iOS自动检测它是否在地理围栏之后重新启动而没有显着的位置变化?

ios swift geolocation core-location geofencing
1个回答
0
投票

首先,你需要在你想要咨询的地区调用requestState(for:),注意这将是一个异步咨询,然后由locationManager(_:didDetermineState:for:)locationManager(_:didEnterRegion:)locationManager(_:didExitRegion:))捕获回复,所以如果你正在咨询一系列地区,请考虑到这一点。

var locationManager = CLLocationManager()
// Do all proper setting for locationManager
...
// Here you make the asynchronous request for the state of a region
locationManager.requestState(for: CLRegion()) // Your region

func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) {
    switch state {
        case .inside:
        // It's inside the region
        case .outside:
        // It's outside
        default:
        // It's unknown
    }
}

总而言之,requestState(for:)相当于在不移动设备的情况下强制进行重大更改,因此所有其他代表也将被调用,但您的兴趣点是locationManager(_:didDetermineState:for:)

资料来源:https://developer.apple.com/documentation/corelocation/cllocationmanager/1423804-requeststate

© www.soinside.com 2019 - 2024. All rights reserved.