NetworkMonitor
public class NetworkMonitor
Use the NetworkMonitor class to determine the current status of the iOS device’s connection to the internet.
To get the type of network connection currently available, use currentNetworkConnection. If the currentNetworkConnection is WWAN, you can narrow down the type further with cellularNetworkType, which shows whether the device is using 4G, 3G, or 2G.
To subscribe to network changes, call startMonitoringNetworkChanges() and add an observer to NotificationCenter.default with NetworkMonitor.networkChangedNotificationName as the notification name. To turn off these notifications, call stopMonitoringNetworkChanges().
-
When using the
startMonitoringNetworkChanges()method, register an observer withNotificationCenterusing thisNotification.Name.Declaration
Swift
public static let networkChangedNotificationName = Notification.Name("NetworkChangedNotification")
-
The type of cellular data network available to the iOS device. The possible values are
4G,3G,2G, andunknown.Declaration
Swift
public var cellularNetworkType: String? -
Detects whether the iOS device is currently connected to the internet via WiFi or WWAN, or if there is no connection.
Declaration
Swift
public var currentNetworkConnection: NetworkConnection
-
Creates a new instance of
NetworkMonitoronly if the current device’s network can be accessed.Declaration
Swift
public init?()
-
Begins monitoring changes in the
currentNetworkConnection.If the device’s connection to the internet changes (WiFi, WWAN, or no connection), a notification will be posted to
NotificationCenter.defaultwithNetworkMonitor.networkChangedNotificationName.To intercept network changes, add an observer to
NotificationCenter.defaultwithNetworkMonitor.networkChangedNotificationNameas the notification name.Declaration
Swift
public func startMonitoringNetworkChanges() -> Bool -
Stops monitoring changes in the
currentNetworkConnectionthat were started bystartMonitoringNetworkChanges().Declaration
Swift
public func stopMonitoringNetworkChanges()
View on GitHub
NetworkMonitor Class Reference