An app will often require a specific network status or context to run whether that's connecting to a specific IP, ensuring only a proper configuration is used, or ensuring the device is online at all. While the AppFramework is not capable of controlling some of these on a system level, it does provide components to access these for informing what the app does.
System-provided configurations
Network configurations are handled and stored by the device and don't normally need to be handled by an app. However, for situations where an app does need to be aware of the network connection status of the device, the Network component provides read-only access to network configurations known to the system, allowing apps to detect these system connections and capabilities.
For many apps, all that is specifically needed from a network is to be online and able to connect to services. All that is needed to check that is the isOnline property, a Boolean value that displays whether or not the device is currently online. This property can be used to alter the behavior of the app to perform alternate functions that don't require network access, or to display an error message if being online is required.
Column {
anchors {
left: parent.left
right: parent.right
bottom: parent.bottom
margins: 20 * AppFramework.displayScaleFactor
}
spacing: 5
Item {
height: 20
width: parent.width
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Sign In"
onClicked: {
if (AppFramework.network.isOnline) {
messageDialogOnline.open();
} else {
messageDialogOffline.open();
}
}
MessageDialog {
id: messageDialogOnline
title: "Network Connection"
text: "This device is currently online."
}
MessageDialog {
id: messageDialogOffline
title: "Network Error"
text: "Please connect to a network to get started."
}
}
}
Network addresses
The NetworkAddress component stores a single network IP address. As well as being used alongside other components to perform actions such as connecting to a host and setting up a server, it also provides properties to determine if the address has a special purpose such as loopback or multicast.
A loopback is one of a specific range of IP addresses intended to send outgoing signals to the same computer, typically for testing purposes. For IPv4 devices, the range of 127.0.0.0 to 127.255.255.255 act as loopback addresses, with the most typical address used being 127.0.0.1 and using the name localhost. For IPv6, the address reserved for loopbacks is typically simplified as ::1, as the full address is very long. The NetworkAddress isLoopback Boolean property will return true if the address stored by the object is a loopback address.
Multicast addresses are a wide range of addresses reserved for multicast communication, where information is addressed to a group of destination computers. In apps built with AppFramework, the most common use of multicast is for User Datagram Protocol (UDP), a transmission protocol that prioritizes speed over reliability, but there are many other uses. For IPv4 devices, the range of multicast addresses is from 224.0.0.0 to 239.255.255.255, while IPv6 multicast addresses use the prefix ff00::/8. However, both of these protocols have sections of these ranges reserved for more specific uses of multicast. The NetworkAddress isMulticast Boolean property will return true if the address stored by the object is any kind of multicast address.
NetworkAddress also contains the isNull property for identifying null addresses not valid for any host or interface and the isInSubnet method to determine if the stored IP address is in a given subnet. These functions can be used to ensure that only IP addresses valid to your app's needs will be passed.