Notify app when connection state changes in background - ios

When app goes in background seems there is no way to use Reachability module to make possible app to be notified about:
Device is disconnected from current network then connects back.
Device is disconnected from current network and connects to a new network.
Device receives a new IP.
These events are important, app must update it's location otherwise user will miss calls, etc.
Update:
App is using setKeepAliveTimeout to trigger each 10 minutes a registration process which will update location, but this is too long.

Related

Sending network request after bluetooth update while iOS app is in background

I have an app that is keeping an active connection to a BLE device. The BLE device is sending data from time to time to the app, and I want the app to send this data to a server.
Note the app can be in foreground, background, the iPhone locked, or any possible state.
What I see is that if the app is in the background (after a while), when iOS awakes the app upon a new BLE update from my hardware device, the network request fails instantaneously (like it's not granting network access).
Does anybody know how to approach this issue? note that UIBackgroundTask doesn't seem to work as they can only be requested from the foreground (right?), and in my case, the app is already in the background when the BLE update hits the phone.
Any ideas?
The approach I finally took is to use a background configured URLSession connection. Instead of making the app responsible to upload the data, with URLSession with a background mode, it will request OS to upload the data and notify the app once done.

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

How to get notified when a previously connected peripheral re-connects later (a connection originating from peripheral)?

So I am building an IoT device and now I am working on battery optimizations. I am able to tell the device to connect to a central it has bonded to. This works(ish). You can observe the device connecting in bluetooth settings, but about 5 seconds later it disconnects. I would assume that since there is nothing happening and nothing asking for it, it drops the connection. I also assume that this is allowed for ANCS. What I want to know is can it be used for anything else? I would like to be able to shut off the device, not have to scan constantly in app, and have the system notify my central manager that the device has connected later (when a user wakes it up.
I know I can call
[_manager retrievePeripheralsWithIdentifiers:identifiers];
to get devices I have connected to before, but I am not seeing a way to attach unconnected ones to the manager so that when they do connect in the background, the app knows about it. Anyone have a solution for this?

How to send data to server when receiving location update in background mode?

My app is in background, and then i receive a location update. The system wake up my app for a very short time that is enough to store the location.
Now i would like also to send this location to my server, but i m afraid that the network connection can take longer time (especially if not on wifi) and that the system will move back my app in sleeping state before i reach the end of the connection.
So how can i send the location to my server?

Get messages from server in real-time

It's generally a common question.
I wonder how do mail apps implement functionality of email-receiving?
I need to implement a simple sync between two devices(for example) using my web service.
Imagine a to do list app.
Is it possible to implement such a functionality: device1 makes an entry,then sends a message to webservice, webservice informs device2 that changes took place and device2 adjusts its data and displays that entry?
On iOS what you want could easily be implement with push notifications.
As soon as the server detects changes that device2 needs to be aware of the server will send a push notification to that device.
After the user views the notification the app should update it self, it would also be a good idea to let the app update it self when coming to the foreground.
The reason for doing it with Push Notification and not polling is that if your app is in the background you can only continue to run a process for 10 min max. You might get around this by adding the background mode to your app, like VOIP, Audio or location. But if you app does not fall in those categories apple might reject your app.
With Push notification the device will receive the notification even if your app isn't running or in background.
Basically there are 2 ways:
polling, each device asks the webserver for changes every N minutes: new todo, delete a todo, change a todo, ... and then each device will adjust. The frequency of the poll depends of the level of real time you are looking for. It can be 1 call every second or every 12 hours or much more.
implement a kind of BOSH protocol: the device opens a connection to the server. The server keeps it open until there something new to send to the device or the connection times out. In that case, the device reopens it.
Option 1 is better for your todo app because you don't need real-time accuracy. The option 2 is better for a chat application where you don't want to wait for the message.

Resources