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 withNotificationCenter
using 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
NetworkMonitor
only 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.default
withNetworkMonitor.networkChangedNotificationName
.To intercept network changes, add an observer to
NotificationCenter.default
withNetworkMonitor.networkChangedNotificationName
as the notification name.Declaration
Swift
public func startMonitoringNetworkChanges() -> Bool
-
Stops monitoring changes in the
currentNetworkConnection
that were started bystartMonitoringNetworkChanges()
.Declaration
Swift
public func stopMonitoringNetworkChanges()