Detecting changes in connectivity
Determining the type of network connection once does little good as the device can lose the connection or join a new network at any time. This means that we need to properly respond to these events in order to provide a good user experience.
Note
Do not rely on the following events being fired when your app starts up for the first time. On some devices, it might take several seconds for the first event to fire; however, in some cases, the events might never fire (specifically, if testing in a simulator).
There are two events our app needs to listen to: the online
event and the offline
event. Their names are indicative of their function, so chances are good you already know what they do.
The online
event is fired when the device connects to a network, assuming it wasn't connected to a network before. The offline
event does the opposite: it is fired when the device loses a connection to a network, but only if the device was previously connected to a network. This...