0

I'm trying to perform a network connectivity check whenever user opens the app or whenever app comes in foreground. Below is the sample code

void ApplicationUI::onFullscreen()
{
    qDebug()<<"Application has entered foreground";
    QNetworkConfigurationManager mgr;
    QList<QNetworkConfiguration> activeConfigs = mgr.allConfigurations(QNetworkConfiguration::Active);
    if (activeConfigs.count() > 0)
    {
           qDebug()<<"Has Internet connection";
       }
       else
       {
           qDebug()<<"No Internet connection";
       }

}

This always prints Has Internet connection even when the network connection is off. Any ideas?

Francis F
  • 3,157
  • 3
  • 41
  • 79

1 Answers1

0

You can use QNetworkConfigurationManager.isOnline().

QNetworkConfigurationManager mgr;
mgr.isOnline();

If you want to get notified about changes of the online state then you also can connect to the QNetworkConfigurationManager::onlineStateChanged(bool isOnline) signal.

connect(mgr, SIGNAL(onlineStateChanged(bool)), this, SLOT(onOnlineStateChanged(bool)));
Oliver Kranz
  • 3,741
  • 2
  • 26
  • 31
  • i tested this on simulator 10.3.1 if (mgr1.isOnline()){ qDebug()<<"Has Internet connection";}else { qDebug()<<"No Internet connection";} and it prints No Internet connection when app launchs and when i switch and come back to the app it prints has internet connection, even when i disconnect the internet from my pc it still shows the log has internet connection – Francis F Mar 17 '15 at 05:49
  • I never used this on the simulator. But the code works on real devices. To test the offline mode you could try to turn off WiFi and the mobile network in the simulator instead of disconnecting your pc from the internet. – Oliver Kranz Mar 17 '15 at 09:10
  • In simulator by default wifi and mobile network shows off, but when I take mobile network setting its shows on and doesnt get turned off. I dont have a device now, is there any other way to see if this code works on simulator? – Francis F Mar 17 '15 at 09:46
  • Hi, I tested this code on device and on first app launch it shows no network even when device has internet connectivity, but from there after it works fine ie if we try to minimize app and toggle with network connectivity. I'm calling this function on onFullscreen() method, is there anything else I have to do to make this work for first app launch? – Francis F Apr 06 '15 at 11:35
  • On my device the code also is working on the first launch of the app. I also tested the code in onFullscreen(). Seems to be different on your device. You could use the signal onlineStateChanged(bool) to be notified about changes of the online state. The code only returns true if the device has a data connection which is shown by the little BlackBerry symbol next to the network symbol in the title bar. – Oliver Kranz Apr 13 '15 at 08:04