Search code examples
iosswiftgeofencing

Sending request when entering geofence and no network connection at that moment


My app listens for 3 geofences while the app is closed. When entering a geofence, the identifier is sent to the back-end. Everything works wonderful but I would like to also handle the case when there is no network connection when entering the geofence. Like waiting with sending the request until there is a connection. This has to work when the the app is closed.

It only has to work on devices with iOS 11 or higher. Waitsforconnectivity does not work, I think because the time window from the geofence is to small. When i start the app that specific request is successfully sended so it does work but not if the app is closed. I have to manually start the app to let the pending request send when there is a connection. I also tried dataTask and uploadTask but these give the same behaviour as with waitsForConnectivity. I also think that is not possible to check if there is a network connection with scheduling a task with a timer in the future? Because the app is closed. Is this even possible? I think the only way to do this is with a remote push notification but running code as a response to a push notification is only allowed with Voip.

I find it hard to believe that this isn't possible since this so easy with a BroadcastReceiver on Android.


Solution

  • Try BackgroundFetch

    You can wake your app with certain periodic interval with this approach, without user interaction. Once your app wakes up, check if you have some pending data to be sent to the server. If yes, send it. If the internet connection is not there, no problem, program your app to wake up at let us say every 5 hours.

    // Fetch data once an hour.
       UIApplication.shared.setMinimumBackgroundFetchInterval(3600)
    

    A simple call like above will wake your app up after 1 hour. Follow the steps in the link and you'll get the idea.