Today, more and more applications require an internet connection. I will cover handling internet connection reachability on iOS using Swift 5 in this article.
Showing network connection status helps user to understand if something went wrong. In most of cases just showing spinner is not enough (it doesn’t indicate that network is down). Especially if loader keep spinning more than a few minutes. Whether it is a problem with the server, loss of internet connection or a problem on the device.
Showing more details and information about network status helps user to understand that there is a problem with connection, fix this problem (if user can) or retry later.
For integration of this approach you will need 1-2 minutes.
Or you might download source here and just use it ☺️
Handle the problem with the internet connection and display warning alert to user or make new query when connection will restore.
For this purpose, I use
Step 1: Install Reachability.swift
To install Reachability.swift with CocoaPods:
1 Make sure CocoaPods is installed.
2. Update your Podfile to include the following:
use_frameworks! pod 'ReachabilitySwift'
If you are (for whatever reason) not using CocoaPods you can install Reachability.swift with Carthage or manually.
Step 2: Handle connection changes
I have created an extension with the default declaration of
removeReachabilityObserver where an object that adopt
ReachabilityObserverDelegate protocol subscribes/unsubscribes internet reachability. Unlike
reachabilityChanged, which requires implementation (and is responsible for handling).
Step 3: Usage of
It’s easy to integrate into different classes and projects.
So, here is example of usage in app:
Thanks for the reading. I hope you learned how to handle internet connection status from this article. You can download gist
Check my articles: how to create own custom xCode Template or basic typography design system for iOS using Swift.
If you have any questions or spot any errors please let me know.