SwiftyReachability is a simple and lightweight network interface manager written in Swift.

Overview

Schermata 2022-09-06 alle 10 04 08

SwiftyReachability is a simple and lightweight network interface manager written in Swift.

Freely inspired by https://github.com/tonymillion/Reachability, with the aim of providing an updated interface that includes all the innovations introduced in the iOS world since its latest update and add new features.

  • Easy to use: no configuration needed, SwiftyReachability is ready to go.
  • Shared: it's based on a single istance shared by the whole app, you can access to the network layer's information at any time and any point.
  • Detailed: it allows you to know the connection type used and in the case of 'Cellular Network' you also know which radio technology is being used.
  • SwiftUI compatible: provides an elegant SwiftUI support that is very easy to use.
  • Multiple Observers: you can define several network Observers at the same time to get information and update your UI.

Installation

You can use Swift Package Manager to add SwiftyReachability to your project.

Add Package Dependency

In Xcode, select File > Add Packages...

Specify the Repository

Copy and paste the following into the search/input box.

https://github.com/antonio-war/SwiftyReachability

Specify options

Use Up to Next Major Version as dependency rule and enter the current SwiftyReachability version. Then click Add Package.


Overview

Connection Status

SwiftyReachability's main purpose is to track device connectivity. Through SwiftyConnectionStatus it is possible to find out if it is online or offline.

enum SwiftyConnectionStatus {
    case online
    case offline
}

Connection Type

When the device is online it may be useful to know information about the connection type. You can do it through SwiftyConnectionType.

enum SwiftyConnectionType {
    case cellular(radioType: SwiftyRadioType)
    case wifi
    case ethernet
}

Radio Type

Unlike original Reachability framework we introduced other information, when the device is connected to a cellular network you can know if it is using a 3G, LT or some other radio type, through SwiftyRadioType.

enum SwiftyRadioType {
    case undefined
    case _2G
    case _3G
    case _4G
    case _5G
}

On some devices, however, this feature is not available, at least for now.


Simple Usage

In case you need to access current information, without the need to be informed about future changes, it's possibile to do so by directly accessing to the shared instance of SwiftyReachability.

let connectionManager = SwiftyReachability.shared
let status = connectionManager.connectionStatus
let type = connectionManager.connectionType      

Observer Usage

When the status of your app needs to be updated based on the connection status you may need an observer. Obviously SwiftyReachability provides all the elements for this type of implementation.

Swift / UIKit

For simple objects or view created with UIKit we provide a protocol oriented solution. The object must conform to the SwiftyReachabilityObserver protocol and implement the required methods. He also has to decide when to start and stop the observation.

class UIKitViewController: UIViewController, SwiftyReachabilityObserver {
    
    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var connectionTypeImage: UIImageView!
    @IBOutlet weak var radioTypeLabel: UILabel!
        
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        startObserving()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        stopObserving()
    }
    
    func didChangeConnectionStatus(_ status: SwiftyConnectionStatus) {
        DispatchQueue.main.async {
            switch status {
                case .online:
                self.statusLabel.text = "Online"
                self.statusLabel.backgroundColor = .systemGreen
                case .offline:
                self.statusLabel.text = "Offline"
                self.statusLabel.backgroundColor = .systemRed
            }
        }
    }
    
    func didChangeConnectionType(_ type: SwiftyConnectionType?) {
        DispatchQueue.main.async {
            guard let connectionType = type else {
                self.connectionTypeImage.isHidden = true
                self.radioTypeLabel.isHidden = true
                return
            }
            
            switch connectionType {
            case .cellular(let radioType):
                self.connectionTypeImage.image = UIImage(systemName: "antenna.radiowaves.left.and.right")
                self.connectionTypeImage.isHidden = false
                self.radioTypeLabel.text = radioType.description
                self.radioTypeLabel.isHidden = false
            case .wifi:
                self.connectionTypeImage.image = UIImage(systemName: "wifi")
                self.connectionTypeImage.isHidden = false
                self.radioTypeLabel.isHidden = true
            case .ethernet:
                self.connectionTypeImage.image = UIImage(systemName: "cable.connector")
                self.connectionTypeImage.isHidden = false
                self.radioTypeLabel.isHidden = true
            }
        }
    }
}

SwiftUI

In this case we have instead created an ad hoc solution with an ObservableObject. It is really easy to use SwiftyReachabilityObserverUI

struct SwiftUIView: View {
    
    @ObservedObject
    var connectionObserver : SwiftyReachabilityObserverUI = SwiftyReachabilityObserverUI()
    
    var body: some View {
        VStack {
            if connectionObserver.connectionStatus == .online {
                Text("Online")
            } else {
                Text("Offline")
            }
            
            if let connectionTypeDescription = connectionObserver.connectionType?.description {
                Text(connectionTypeDescription)
            }
        }
    }
}

Advice

Like any other package related to Reachability, in the case simulator execution, there may be delays in the functioning of the network observers. We therefore recommend testing your code on devices, where these problems do not occur.


License

SwiftyCache is published under the Apache 2.0 license.

You might also like...
iOS Network monitor/interceptor framework written in Swift
iOS Network monitor/interceptor framework written in Swift

NetShears NetShears is a Network interceptor framework written in Swift. NetShears adds a Request interceptor mechanisms to be able to modify the HTTP

Network abstraction layer written in Swift.
Network abstraction layer written in Swift.

Moya 15.0.0 A Chinese version of this document can be found here. You're a smart developer. You probably use Alamofire to abstract away access to URLS

Alamofire Network Layer written in swift 5 using the protocol oriented, combine, UIKit, MVVM.

CoreAPI-iOS This project Contains Alamofire Network layer Based on Protocol Oriented Concept and Combine Framework. It is created with UIKit, Alamofir

Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux.

BlueSocket Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux. Prerequisites Swift Swift Open Source swift-5.1

NSURLSession network abstraction layer, using Codable and Decodable for response and Encodable for request. ⚙️🚀
NSURLSession network abstraction layer, using Codable and Decodable for response and Encodable for request. ⚙️🚀

SONetworking NSURLSession network abstraction layer, using Codable and Decodable for response and Encodable for request. Project Folder and File Struc

Swift Paging is a framework that helps you load and display pages of data from a larger dataset from local storage or over network.

Swift Paging is a framework that helps you load and display pages of data from a larger dataset from local storage or over network. This approach allows your app to use both network bandwidth and system resources more efficiently. It's built on top of Combine, allowing you to harness its full power, handle errors easily, etc.

Setup your class structure in Xcode Interface Builder and save() in Parse Server.
Setup your class structure in Xcode Interface Builder and save() in Parse Server.

ISParseBind With ISParseBind you can save, update and query PFObjects using the power of Xcode Interface Builder resources. https://www.youtube.com/wa

A peer to peer framework for OS X, iOS and watchOS 2 that presents a similar interface to the MultipeerConnectivity framework

This repository is a peer to peer framework for OS X, iOS and watchOS 2 that presents a similar interface to the MultipeerConnectivity framework (which is iOS only) that lets you connect any 2 devices from any platform. This framework works with peer to peer networks like bluetooth and ad hoc wifi networks when available it also falls back onto using a wifi router when necessary. It is built on top of CFNetwork and NSNetService. It uses the newest Swift 2's features like error handling and protocol extensions.

DBNetworkStack is a network abstraction for fetching request and mapping them to model objects

DBNetworkStack Main Features 🛡 Typed network resources 🏠 Value oriented architecture 🔀 Exchangeable implementations 🚄 Extendable API 🎹 Composable

Releases(1.0.1)
Owner
Antonio Guerra
iOS Developer | Machine Learning & Big Data Student
Antonio Guerra
Easy and lightweight network layer for creating different set of network requests like GET, POST, PUT, DELETE customizable with coders conforming to TopLevelDecoder, TopLevelEncoder

Easy and lightweight network layer for creating different set of network requests like GET, POST, PUT, DELETE customizable with coders conforming to TopLevelDecoder, TopLevelEncoder

Igor 2 Sep 16, 2022
A new, clean and lean network interface reachability library written in Swift.

Reachability A new, clean and lean network interface reachability library written in Swift. Remarks Network reachability changes can be monitored usin

Alecrim 7 Aug 8, 2022
Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application.

Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application. Install Carthage

Ronan Rodrigo Nunes 89 Dec 26, 2022
NWReachability - a pure Swift library for monitoring the network connection of iOS devices using Apple's Network framework.

NWReachability is a pure Swift library for monitoring the network connection of iOS devices using Apple's Network framework.

null 4 Dec 2, 2022
A network extension app to block a user input URI. Meant as a network extension filter proof of concept.

URIBlockNE A network extension app to block a user input URI. Meant as a network extension filter proof of concept. This is just a research effort to

Charles Edge 5 Oct 17, 2022
Lightweight network abstraction layer, written on top of Alamofire

TRON is a lightweight network abstraction layer, built on top of Alamofire. It can be used to dramatically simplify interacting with RESTful JSON web-

MLSDev 528 Dec 26, 2022
A lightweight but powerful network library with simplified and expressive syntax based on AFNetworking.

XMNetworking English Document XMNetworking 是一个轻量的、简单易用但功能强大的网络库,基于 AFNetworking 3.0+ 封装。 其中,XM 前缀是我们团队 Xcode-Men 的缩写。 简介 如上图所示,XMNetworking 采用中心化的设计思想

KANGZUBIN 981 Dec 29, 2022
A lightweight, one line setup, iOS / OSX network debugging library! 🦊

Netfox provides a quick look on all executed network requests performed by your iOS or OSX app. It grabs all requests - of course yours, requests from

Christos Kasketis 3.4k Dec 28, 2022
Network abstraction layer written in Swift.

Moya 14.0.0 A Chinese version of this document can be found here. You're a smart developer. You probably use Alamofire to abstract away access to URLS

Moya 14.4k Jan 1, 2023
A generic network layer written in swift

SwiftyNet 1.0.0 A generic network layer written in swift. you can use it as an abstraction layer above Alamofire with generic returned types. Installa

Mohamed Salah Zidane 17 Oct 11, 2021