In this article, learn how to make sure your Swift application handles internet connection reachability events when they happen.
More often than not, mobile applications need an active internet connection to function properly. It is normal, however, for the internet connection to be lost. In cases like these, it is up to the developer to come up with ways to make the experience bearable, or in the least, notify the user.
In this article, we are going to see how we can detect internet connection issues in Swift, and some ways we can handle it.
Here is the sample application we will be building and how it handles different internet connectivity scenarios:
For you to be able to follow along in this article, you will need the following requirements:
When you have the above requirements, let’s dive in.
Before we begin, we will create a playground. This is where we will write all our use cases and handle them.
Swift comes with its own Reachability implementation for detecting connection issues, but we will be using a third-party library. We are doing this because it is easier and the API is more expressive than the one built-in.
Open Xcode and set up a new project.
This project will be a simple playground that we can experiment with.
To detect when the connection goes offline we are going to be using the Reachability.swift ****package. It is a “replacement for Apple’s Reachability re-written in Swift with closures”.
Open your terminal and run the command below:
1$ pod init
This will create a new Podfile
where we can declare the Cocoapods dependencies. Open the Podfile
and replace the contents with the code below:
1platform :ios, '9.0'
2
3 target 'project_name' do
4 use_frameworks!
5
6 pod 'ReachabilitySwift'
7 pod 'Alamofire'
8 end
? You need to replace
**project_name**
with the name of your project.
Save the file and run the command below to install the Pods to your project:
1$ pod install
When the installation is complete, open the *.xcworkspace
file in the root of your project. This will launch Xcode.
Create a new NetworkManager
class. This class will store the network status and be a simple proxy to the Reachability
package. In the file, paste the code below:
1import Foundation
2 import Reachability
3
4 class NetworkManager: NSObject {
5
6 var reachability: Reachability!
7
8 // Create a singleton instance
9 static let sharedInstance: NetworkManager = { return NetworkManager() }()
10
11
12 override init() {
13 super.init()
14
15 // Initialise reachability
16 reachability = Reachability()!
17
18 // Register an observer for the network status
19 NotificationCenter.default.addObserver(
20 self,
21 selector: #selector(networkStatusChanged(_:)),
22 name: .reachabilityChanged,
23 object: reachability
24 )
25
26 do {
27 // Start the network status notifier
28 try reachability.startNotifier()
29 } catch {
30 print("Unable to start notifier")
31 }
32 }
33
34 @objc func networkStatusChanged(_ notification: Notification) {
35 // Do something globally here!
36 }
37
38 static func stopNotifier() -> Void {
39 do {
40 // Stop the network status notifier
41 try (NetworkManager.sharedInstance.reachability).startNotifier()
42 } catch {
43 print("Error stopping notifier")
44 }
45 }
46
47 // Network is reachable
48 static func isReachable(completed: @escaping (NetworkManager) -> Void) {
49 if (NetworkManager.sharedInstance.reachability).connection != .none {
50 completed(NetworkManager.sharedInstance)
51 }
52 }
53
54 // Network is unreachable
55 static func isUnreachable(completed: @escaping (NetworkManager) -> Void) {
56 if (NetworkManager.sharedInstance.reachability).connection == .none {
57 completed(NetworkManager.sharedInstance)
58 }
59 }
60
61 // Network is reachable via WWAN/Cellular
62 static func isReachableViaWWAN(completed: @escaping (NetworkManager) -> Void) {
63 if (NetworkManager.sharedInstance.reachability).connection == .cellular {
64 completed(NetworkManager.sharedInstance)
65 }
66 }
67
68 // Network is reachable via WiFi
69 static func isReachableViaWiFi(completed: @escaping (NetworkManager) -> Void) {
70 if (NetworkManager.sharedInstance.reachability).connection == .wifi {
71 completed(NetworkManager.sharedInstance)
72 }
73 }
74 }
In the class above, we have defined a couple of helper functions that will help us get started with network status monitoring. We have a sharedInstance
that is a singleton and we can call that if we do not want to create multiple instances of the NetworkManager
class.
In the init
method, we create an instance of Reachability
and then we register a notification using the NotificationCenter
class. Now, every time the network status changes, the callback specified by NotificationCenter
(which is networkStatusChanged
) will be called. We can use this to do something global that is activated when the network is unreachable.
We have defined other helper functions that will generally make running code depending on the status of our internet connection a breeze. We have *isReachable*
, *isUnreachable*
, *isReachableViaWWAN*
and *isReachableViaWiFi*
.
The usage of one of these helpers will generally look like this:
1NetworkManager.isReachable { networkManagerInstance in 2 print("Network is available") 3 } 4 5 NetworkManager.isUnreachable { networkManagerInstance in 6 print("Network is Unavailable") 7 }
⚠️ This is not an event listener and will only run once. To use a listener to pick up network changes in real-time, you’ll need to use
**NetworkManager.sharedInstance.reachability.whenReachable**
**. We will show an example later in the article.**
Now that we have a manager class, let’s see how we can use this in an application.
Sometimes, your application relies heavily on an internet connection and you need to detect the status on launch. Let’s see how we can handle this using the NetworkManager
class.
Create a new controller called LaunchViewController
. We will treat the first controller view on the storyboard as the launch controller. We will try to detect if the user’s device is online and, if not, we will create an offline page to handle this so the user does not get into the application at all.
In the LaunchController
replace the contents with the following code:
1import UIKit
2
3 class LaunchViewController: UIViewController {
4
5 let network: NetworkManager = NetworkManager.sharedInstance
6
7 override func viewDidLoad() {
8 super.viewDidLoad()
9
10 // If the network is unreachable show the offline page
11 NetworkManager.isUnreachable { _ in
12 self.showOfflinePage()
13 }
14 }
15
16 private func showOfflinePage() -> Void {
17 DispatchQueue.main.async {
18 self.performSegue(withIdentifier: "NetworkUnavailable", sender: self)
19 }
20 }
21 }
In this class, we use our NetworkManager
‘s *isUnreachable*
method to fire the showOffline
method when the network is unavailable. Let us create that view controller. Create a new view controller called OfflineViewController
.
Open the Main.storyboard
file and set the custom class of the first view to LaunchViewController
.
Next, create a new view controller in the storyboard. Set the OfflineViewController
as the custom class for this new view controller. Now create a manual segue called NetworkUnavailable
between the new view controller and the LaunchViewController
. When you are done you should have something similar to this:
Now let’s run the application. Note, though, that before you run your application, your development machine should be offline as the iOS simulator uses the machine’s internet connection. When you run the application, you should get the offline page we created.
Now let us create a view controller that shows up when there is a connection.
Now that we have created an Offline View Controller and it works when the device is offline, let us handle what happens when the device is back online.
Create a new navigation view controller on the storyboard below the Offline View Controller. We will create a controller that displays the latest Reddit posts. Create a new view controller class called PostsTableViewController
. Now make this the custom class for the view controller attached to the Navigation View Controller.
Now create a manual segue called MainController
from the Navigation View Controller to the Launch View Controller and the Offline View Controller. You should have something similar to this:
Now, open the LaunchViewController
class and at the bottom of the viewDidLoad
method add the following:
1NetworkManager.isReachable { _ in 2 self.showMainPage() 3 }
Then add the method below to the controller:
1private func showMainPage() -> Void { 2 DispatchQueue.main.async { 3 self.performSegue(withIdentifier: "MainController", sender: self) 4 } 5 }
This will make sure that when the app is launched, it will check for connectivity and then, if the connection is available, it will present the PostsTableViewController
otherwise it will present the OfflineViewController
.
Great! But what happens when the user has hit the OfflineViewController
and then the network comes back online? Let’s handle that scenario.
Open the OfflineViewController
and replace the code with the code below:
1import UIKit
2
3 class OfflineViewController: UIViewController {
4
5 let network = NetworkManager.sharedInstance
6
7 override func viewDidLoad() {
8 super.viewDidLoad()
9
10 // If the network is reachable show the main controller
11 network.reachability.whenReachable = { _ in
12 self.showMainController()
13 }
14 }
15
16 override func viewWillAppear(_ animated: Bool) {
17 super.viewWillAppear(animated)
18
19 // Hide the navigation bar
20 navigationController?.setNavigationBarHidden(true, animated: animated)
21 }
22
23 override func viewWillDisappear(_ animated: Bool) {
24 super.viewWillDisappear(animated)
25
26 // Show the navigation bar
27 navigationController?.setNavigationBarHidden(false, animated: animated)
28 }
29
30 private func showMainController() -> Void {
31 DispatchQueue.main.async {
32 self.performSegue(withIdentifier: "MainController", sender: self)
33 }
34 }
35 }
In the controller above, you can see, in the viewDidLoad
method, that we set the whenReachable
completion to show the main controller. This means that, as long as its offline, you watch for when the device comes back online. When it does, present the PostsTableViewController
.
We also override the viewWillAppear
and viewWillDisappear
methods to ensure the navigation bar does not show on the Offline View Controller.
Fetching Posts from Reddit API in Swift
Now let us add the logic that’ll fetch data from Reddit and display on our PostsTableViewController
. Open the file and replace the contents with the code below:
1import UIKit
2 import Alamofire
3
4 struct RedditPost {
5 let title: String!
6 let subreddit: String!
7 }
8
9 class PostsTableViewController: UITableViewController {
10
11 var posts = [RedditPost]()
12 let network = NetworkManager.sharedInstance
13
14 override func viewDidLoad() {
15 super.viewDidLoad()
16
17 navigationItem.title = "Latest Posts"
18
19 // Fetch the posts and then reload the table
20 fetchPosts { posts in
21 self.posts = posts
22 self.tableView.reloadData()
23 }
24 }
25
26 private func fetchPosts(completion: @escaping (_ posts: [RedditPost]) -> Void) -> Void {
27 // Send a request to the Reddit API
28 Alamofire.request("https://api.reddit.com").validate().responseJSON { response in
29 switch response.result {
30 case .success(let JSON):
31 let data = JSON as! [String:AnyObject]
32
33 guard let children = data["data"]!["children"] as? [AnyObject] else { return }
34
35 var posts = [RedditPost]()
36
37 // Loop through the Reddit posts and then assign a post to the posts array
38 for child in 0...children.count-1 {
39 let post = children[child]["data"] as! [String: AnyObject]
40
41 posts.append(RedditPost(
42 title: post["title"] as! String,
43 subreddit: "/r/" + (post["subreddit"] as! String)
44 ))
45 }
46
47 DispatchQueue.main.async {
48 completion(posts)
49 }
50
51 case .failure(let error):
52 print(error)
53 }
54 }
55 }
56
57 override func didReceiveMemoryWarning() {
58 super.didReceiveMemoryWarning()
59 }
60
61 // MARK: - Table view data source
62
63 override func numberOfSections(in tableView: UITableView) -> Int {
64 return 1
65 }
66
67 // Return the number of posts available
68 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
69 return self.posts.count
70 }
71
72 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
73 let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath)
74 let post = posts[indexPath.row] as RedditPost
75
76 cell.textLabel?.text = post.title
77 cell.detailTextLabel?.text = post.subreddit
78
79 return cell
80 }
81 }
In the fetchPosts
method, we use Alamofire
to send a GET request to the Reddit API. We then parse the response and add it to the RedditPost
struct we created at the top of the file. This makes the data we are passing to the tableView
consistent.
Now, let us handle one more scenario. Imagine while viewing the latest Reddit posts, you lose connectivity. What happens? Let’s show the offline page again when that happens.
As was previously done, create a manual segue called NetworkUnavailable
from the PostsTableViewController
to the OfflineViewController
. Now add this code to the bottom of the viewDidLoad
method:
1network.reachability.whenUnreachable = { reachability in
2 self.showOfflinePage()
3 }
Now add the method below to the controller:
1private func showOfflinePage() -> Void { 2 DispatchQueue.main.async { 3 self.performSegue(withIdentifier: "NetworkUnavailable", sender: self) 4 } 5 }
This will listen for when the device goes offline and, if that happens, it will showOfflinePage
.
That’s all! We have been able to handle offline and online events using our NetworkManager in Swift.
In this article, we considered how to make sure your application handles online and offline events when they happen. You can always implement this any how you wish. If you have any questions or feedback, leave them below in the comments.
The source code to this playground is available on GitHub.