QNCM -> QNetworkInformation

mobile: ONLINE vs OFFLINE

In mobile apps it’s always important to know if you’re online or offline. I’m using this in simple or complex workflows. Following examples HowTo switch from QNCM to QNetworkInformation are from one of my projects: QtWS Conference App – Here’s the 5.15 repo – 6.6 repo will follow soon. Your workflows probably will be similar and hopefully my examples will give you some hints what must be done.

There were already DEPRECATION warnings in 5.15, so we’re prepared for needed refactorings.

QNetworkConfigurationManager to QNetworkInformation

The QNetworkConfigurationManager (QNCM) and QNetworkConfiguration Classes are obsolete in Qt 6. But there’s a new one with more features in Qt 6: QNetworkInformation.

If you’re on windows you remember all the trouble with Bearer management and DLLs ? All of this is gone, because Bearer management was removed and all was done under the hood from Qt default plugins.

Unfortunately you cannot simply change an import – you have to rewrite your code, but the new QNetworkInformation is much better, so it’s worth the work 🙂

iOS Reachability Code

In Qt 5.15 I had some issues with QNetworkConfigurationManager (QNCM) and detecting ‘isOnline’ under iOS, where in some cases online is reported, but device is offline. Even in airplane mode isOnline reports true. See QTBUG-56151 and QTBUG-58946. So I added iOS Reachability classes instead.

Now with QNetworkInformation Qt uses Apple’s implementation under the hood. So we can remove platform-specific code 🙂

Remove iOS reachability classes from .pro:

We don’t need the framework SystemConfiguration or ObjectiveC sources (Reachability.mm, ReachabilityListener.mm) anymore.

Delete ios src files:

Remove from C++ Code:

Also remove corresponding code from .cpp:

QNCM -> QNetworkInformation

Remove QNCM from code and use QNetworkInformation instead. Changes in my dataserver.hpp are easy:

void onOnlineStateChanged(QNetworkInformation::Reachability newReachability);

I don’t want to go deep into all the changes in DataServer.cpp – here are only some important snippets:

QNetworkInformation is a singleton and stays alive from the first successful load() until destruction of the QCoreApplication object.

Don’t instantiate the QNetworkInformation Class, always use QNetworkInformation::instance().

There should always be a backend, so it’s a good idea to test this:

bool backendFound = QNetworkInformation::instance()->loadDefaultBackend();

QNetworkInformation::instance()->reachability() replaces QNAM::networkAccessible() function

Test for Reachability, per ex. QNetworkInformation::Reachability::Local || ...Reachability::Site || ...Reachability::Online to detect if the App is online.

In Qt 5.15 we got the onlineStateChanged(bool) SIGNAL from QNCM – now we must watch reachabilityChanged(QNetworkInformation::Reachablility) SIGNAL from QNetworkInformation.

connect(QNetworkInformation::instance(), &QNetworkInformation::reachabilityChanged, this, &DataServer::onOnlineStateChanged)

The new QNetworkInformation Class is more powerful then QNCM and we get some more signals and more detailed infos:

  • Reachability
    • Unknown
    • Disconnected
    • Local – connected to network, access to devices on local network
    • Site – connected to network, access to devices on local subnet or intranet
    • Online – access to the Internet
  • TransportMedium
    • Unknown
    • Ethernet
    • Cellular
    • WiFi
    • Bluetooth
  • is behind Captive Portal
  • is Metered

It depends from your workflow and business rules, HowTo decide if the App is online, which means Server Requests are allowed. We’ll need some networkSettings to configure the behavior. Now as a very first step, ekke’s Apps emit the SIGNAL isOnline() if reachability is Local, Site or Online. There will be a new example App ekke’s OnlineChecker Example sooner or later.

Network Info Popup

It’s a good idea to provide a NetworkInfoPopup, if there are problems with Network state at customer sites From QtWS App per ex. you can open this from a button in TitleBar.

    Q_INVOKABLE
    QVariantMap networkInfo();

Will take some time until src of QtWS App for 6.6 will become public, so I added a Gist HowTo create a QVariantMap with all the relevant infos and also a sample PopupNetworkInfo.

Here’s the new Popup from an App running on Android, iOS, macOS in WiFi.

PopupNetworkInfo Android Example:

PopupNetworkInfo iOS Example:

PopupNetworkInfo macOS Example:

You can follow all the needed changes in src from Github repo of QtWS. coming soon.

more changes – docs about Network Changes

There are more things to watch: see also this Qt Changes to Network docs.