简体   繁体   中英

iOS RegionMonitoring without significant location change after device reboot

I'm trying to implement the functionality that it requires detect whether the device is inside of target region(geofence) or not. If it is inside of the region, it should execute some task no matter it is in foreground or background.

I was able to figure out most of the parts and implemented.

However the challenge is the case where user reboot the device while already inside of the region and does not make any significant location change.

In that case, it seems like iOS does not detect whether it is inside or outside. Thus my app does nothing instead of executing the task.

So is there any way for iOS to automatically detect whether it is inside of geofence or not after rebooting without significant location change?

First you need to call requestState(for:) on the region you wish to consult, notice this will make an asynchronous consult which reply will then catch by locationManager(_:didDetermineState:for:) ( locationManager(_:didEnterRegion:) and locationManager(_:didExitRegion:) ) so if you're consulting an array of regions take that into account.

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
    }
}

In summary, requestState(for:) is the equivalent of forcing a significant change without moving the device so all the other delegates will be called as well but your point of interest is locationManager(_:didDetermineState:for:)

Source: https://developer.apple.com/documentation/corelocation/cllocationmanager/1423804-requeststate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM