Network Communication
NetworkConnectivityCheck
Use NetworkConnectivityCheck
to easily register an observer to the API and receive information about the current network status of your device.
Usage
You have two options when working with the NetworkConnectivityCheck
. You can:
- Register a class as an observer at the
ConnectivityReceiver
to receive consistent updates about the status of your device’s reachability. To use theConnectivityReceiver
you have to implement theConnectivityObserver
protocol in your class and implement the given functions. - Check the reachability manually with the
ConnectivityUtils
class.
// register observer to start the Monitoring
ConnectivityReceiver.registerObserver(self)
// MARK: Protocol Functions
func connectionEstablished() {
logger.debug("Connection established")
// do something useful
}
func connectionChanged(_ previousReachabilityType: ReachabilityType, reachabilityType: ReachabilityType) {
logger.debug("connection changed")
// do something useful
}
func connectionLost() {
logger.debug("Connection lost")
// do something useful
}
You should always unregister your observers to prevent memory leaks:
// unregister observer, if observer list is empty the monitoring stops
ConnectivityReceiver.unregisterObserver(self)
You can check the actual network status of your device manually with the help of the ConnectivityUtils
class:
// This example checks the status manually inside IBActions
@IBOutlet weak var manuallyConnectivityLabel: UILabel!
@IBAction func checkMobile(_ sender: UIButton) {
if ConnectivityUtils.isMobileConnected() {
manuallyConnectivityLabel.text = "Mobile connected!"
} else {
manuallyConnectivityLabel.text = "Mobile not connected!"
}
}
@IBAction func checkWiFi(_ sender: UIButton) {
if ConnectivityUtils.isWiFiConnected() {
manuallyConnectivityLabel.text = "WiFi connected!"
} else {
manuallyConnectivityLabel.text = "WiFi not connected!"
}
}
@IBAction func checkOnline(_ sender: UIButton) {
if ConnectivityUtils.isConnected() {
manuallyConnectivityLabel.text = "Internet connected!"
} else {
manuallyConnectivityLabel.text = "Internet not connected!"
}
// or ask if a specific `ReachabilityType`is connected.
if ConnectivityUtils.isConnected(ReachabilityType.wifi) {
// Connected via WiFi
}
}
NetworkConnectivityCheck component Logger ID
This component uses the following name prefix for logging: ‘SAP.Foundation.NetworkConnectivityCheck’
-
Observer protocol for receiving information about connectivity type changes.
See moreDeclaration
Swift
public protocol ConnectivityObserver : AnyObject
-
The ConnectivityReceiver informs all registered ConnectivityObservers about connectivity type changes.
To use the ConnectivityReceiver you have to implement the
ConnectivityObserver
protocol in your class and implement the given functions.// register observer to start the Monitoring ConnectivityReceiver.registerObserver(self) // MARK: Protocol Functions func connectionEstablished() { logger.debug("Connection established") // do something useful } func connectionChanged(_ previousReachabilityType: ReachabilityType, reachabilityType: ReachabilityType) { logger.debug("connection changed") // do something useful } func connectionLost() { logger.debug("Connection lost") // do something useful }
You always should unregister your observers to prevent memory leaks.
See more// unregister observer, if observer list is empty the monitoring stops ConnectivityReceiver.unregisterObserver(self)
Declaration
Swift
public class ConnectivityReceiver
-
A utils class to determine the connectivity status.
If you want to check the actual network status of your device manually you can do this with the help of the
ConnectivityUtils
class.
See more// This example checks the status manually inside IBActions @IBOutlet weak var manuallyConnectivityLabel: UILabel! @IBAction func checkMobile(_ sender: UIButton) { if ConnectivityUtils.isMobileConnected() { manuallyConnectivityLabel.text = "Mobile connected!" } else { manuallyConnectivityLabel.text = "Mobile not connected!" } } @IBAction func checkWiFi(_ sender: UIButton) { if ConnectivityUtils.isWiFiConnected() { manuallyConnectivityLabel.text = "WiFi connected!" } else { manuallyConnectivityLabel.text = "WiFi not connected!" } } @IBAction func checkOnline(_ sender: UIButton) { if ConnectivityUtils.isConnected() { manuallyConnectivityLabel.text = "Internet connected!" } else { manuallyConnectivityLabel.text = "Internet not connected!" } // or ask if a specific `ReachabilityType`is connected. if ConnectivityUtils.isConnected(ReachabilityType.wifi) { // Connected via WiFi } }
Declaration
Swift
public struct ConnectivityUtils
-
The ReachabilityType enum defines a set of Reachability Types which are representing the actual status of the devices reachability.
See moreDeclaration
Swift
public enum ReachabilityType : CustomStringConvertible