قالب وردپرس درنا توس
Home / IOS Development / Network Reachability in Swift

Network Reachability in Swift



Almost every mobile app needs to connect to the Internet at some point in order to retrieve data from a host or service or upload new data to it. However, the internet connection is not always available and availability can be changed at any time. In order to know the state of the system's current network and whether a host or service can be accessed through this network, we can use the SCNetworkReachability API.

This API is part of the Core Foundation, so it is written in C. If you use Swift, this API is not very good to use for many people. Apple gives some sample code that contains a class called Reachability which is a Objective-C wrapper around SCNetworkReachability API. This class encapsulates how to access the network, along with some practical methods to ask if we are connected to or not to the Internet. It also allows to register your items to NSNotificationCenter to be notified through an item NSNotification when the network status changes. You can find this packing class in a copy project available at:

https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html

If you are already familiar with Reachability class, you should only update your code with the latest version (4.2 of November 1

1, 2015) that fixes a couple of errors that could potentially leak some memory.

The sample code provided by Apple is written in Objective-C, and since we get many petitions from developers requesting the Swift version, we will review it in this post. We want to write our own Reachability class in Swift 3.0 and explain how it works step by step.

SCNetworkReachability API

The SCNetworkReachability API provides a synchronous method for determining accessibility. The synchronous method allows us to request the current status of accessibility by calling the feature SCNetworkReachabilityGetFlags . The second parameter for this feature is a memory stick that will be filled with a flag describing the accessibility status and will provide additional information, for example, if a connection is required and if any user intervention is required when establishing a connection.

In addition to the synchronous method, the SCNetworkReachability API provides an asynchronous method. To implement this approach, we need to plan the item SCNetworkReachability in a runway. Offers callback feature, we can then enter a notification when access to the remote host changes.

Let's do it in Swift

Start Xcode 8 and create a new Swift Single View Application project. Name the ReachabilityExample . Let's immediately add a new swift file to our project. Name the Reachability.swift . Let's add the following file to the following file:

We will inform our App when the network status changes in three possible scenarios.

  1. when the app is offline,
  2. when the app is connected via Wifi,
  3. when the app is connected via WWAN.

We will do so by sending a notice containing the status of the connection. So let's define the warning name and the three possible states:

Let us now implement Reachability

class Reachability : NSObject {

}

Let's add this property to save SCNetworkReachability