A simple class to check for internet connection availability in Swift.

Overview

Reach

A simple class to check for internet connection availability in Swift. Works for both 3G and WiFi connections.

Install

Manually
  • Add the Reach.swift file to your project.

Usage

There are two ways to get network status information from Reach.

  1. Call Reach().connectionStatus(). The network status is returned in an enum called ReachabilityStatus.
let status = Reach().connectionStatus()

switch status {
case .unknown, .offline:
    print("Not connected")
case .online(.wwan):
    print("Connected via WWAN")
case .online(.wiFi):
    print("Connected via WiFi")
}
  1. By subscribing to ReachabilityStatusChangedNotifications. The network status is returned as a string.
override func viewDidLoad() {
    super.viewDidLoad()
    
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.networkStatusChanged(_:)), name: Notification.Name(rawValue: ReachabilityStatusChangedNotification), object: nil)
    
    Reach().monitorReachabilityChanges()
}

@objc func networkStatusChanged(_ notification: Notification) {
    if let userInfo = notification.userInfo {
        let status = userInfo["Status"] as! String
        print(status)
    }
    
}

ToDo

  • Return storngly typed object containing more information about the network status.

Credits

  • Chris Danielson is the author of the original code written in Objective-C.
  • Martin R from StackOverflow helped me immensely in converting C code to Swift.
Comments
  • "Use of unresolved identifier 'Reachability'"

    I have downloaded the code from "https://github.com/Isuru-Nanayakkara/Swift-Reachability" and I have to integrate the Reachability in my app but I am getting the error "Use of unresolved identifier 'Reachability'". The problem is very confusing to me because in one of my View Controller I am able to access the function if Reachability.isConnectedToNetwork() {

    } but in another View Controller it is giving error Use of unresolved identifier 'Reachability' I have been trying to understand the cause of this issue but I didn't find any solution. Please provide me solution ASAP.

    opened by harshcs 5
  • Cannot invoke 'withUnsafePointer' HELP

    Cannot invoke 'withUnsafePointer' HELP

    So I'm getting this error and I can't find a solution, help.

    Cannot invoke 'withUnsafePointer' with an argument list of type '(inout sockaddr_in, () -> _)

    let defaultRouteReachability = withUnsafePointer(&zeroAddress)
    
    opened by geennari 2
  • Apple Match-O Librarian Error

    Apple Match-O Librarian Error

    I am iOS 8.2 using CocoaPods. What's missing here?

    error: /Applications/Xcode-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: unknown option character `X' in: -Xlinker Usage: /Applications/Xcode-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static [-] file [...] [-filelist listfile[,dirname]] [-arch_only arch] [-sacLT] [-no_warning_for_no_symbols] Usage: /Applications/Xcode-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -dynamic [-] file [...] [-filelist listfile[,dirname]] [-arch_only arch] [-o output] [-install_name name] [-compatibility_version #] [-current_version #] [-seg1addr 0x#] [-segs_read_only_addr 0x#] [-segs_read_write_addr 0x#] [-seg_addr_table ] [-seg_addr_table_filename <file_system_path>] [-all_load] [-noall_load]

    opened by fabnoe 2
  • Create a release/tag to use as cocoapods dependency

    Create a release/tag to use as cocoapods dependency

    Hi,

    While cocoapods 0.36 doesn't have an official release, it would be nice to have a tag based on master branch and then add Swift-Reachability as reference in my project.

    Like podspec says, I believe the tag should be named 0.1.0, just guessing, if not, please make sure both match with the same version.

    opened by ararog 2
  • Added new method to check on which type the connection has

    Added new method to check on which type the connection has

    Sometimes you need to distinguish between Wifi and Mobile Connection.

    I added the method isConnectedToNetworkOfType() -> ReachabilityType Which returns an enum value of the type: enum ReachabilityType { case WWAN, WiFi, NotConnected }

    Also enhanced the example for it

    opened by arnoappenzeller 1
  • Notifications did not work as expected.

    Notifications did not work as expected.

    Little experiment on XCODE 11.5

    Add Reach.swift to project

    Add code

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.networkStatusChanged(_:)), name: Notification.Name(rawValue: ReachabilityStatusChangedNotification), object: nil)
    		Reach().monitorReachabilityChanges()
    

    to viewDidLoad() of ViewController


    Turn Network Off unplugging net cable and switch off Wi-Fi connection on Mac

    Build and start project in Simulator

    We get string "Offline" on output. Everything is fine right now.

    Switch internet connection On connecting to WiFi. Run some net activity in project. Download some data successfully. Nothing happen in NotificationCenter about ReachabilityStatusChangedNotification.


    Another scenario with same code.

    Turn Network On unplugging net cable but switch Wi-Fi connection On

    Build and start project in Simulator

    We get string "Online" on output. Everything is fine right now. Switch Wi-Fi off. We get string "Offline" on output. Everything is perfect! But Reconnect Wi-Fi. Run some net activity in project. Download some data successfully. Ooops. Nothing happen in NotificationCenter about ReachabilityStatusChangedNotification again.

    opened by danilamaster 1
  • IPV6 Apple rejection

    IPV6 Apple rejection

    Hi, i have some issues with Apple using this Framework. Apple says that my application is crashing when in IPV6 netowrk. Im using Reach framework and all works fine but Apple says no. Please if someone could help me.

    opened by programmer2710 1
  • Internet connection check

    Internet connection check

    Hi,

    I'm currently connected to a wi-fi network but it doesn't connect to the internet. Nevertheless your implementation still returns "Online" for connectionStatus. Can you please help me finding out what is wrong? However it works properly when really exists internet connection or when the wi-fi is turned off.

    Thanks

    opened by arildojr 0
  • Add shorthand method to check if status is one of .online

    Add shorthand method to check if status is one of .online

    I think it is a frequent case where it is sufficient to know if you are online at all without caring for the connection type, so I've added a helper method for the ReachabilityStatus to tell if its one of the .online options

    opened by mkilmanas 0
  • Make ReachabilityStatus Equatable

    Make ReachabilityStatus Equatable

    I was unable to compare is returned status is a particular one - I would get an error like this screen shot 2016-11-29 at 09 02 18 So I've added mysqlf an Equatable protocol and its implementation to ReachabilityStatus

    opened by mkilmanas 0
Owner
Isuru Nanayakkara
Isuru Nanayakkara
🌐 Makes Internet connectivity detection more robust by detecting Wi-Fi networks without Internet access.

Connectivity is a wrapper for Apple's Reachability providing a reliable measure of whether Internet connectivity is available where Reachability alone

Ross Butler 1.6k Dec 30, 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
Bonjour networking for discovery and connection between iOS, macOS and tvOS devices.

Merhaba Bonjour networking for discovery and connection between iOS, macOS and tvOS devices. Features Creating Service Start & Stop Service Stop Brows

Abdullah Selek 67 Dec 5, 2022
❌📱 A little swift Internet error status indicator using ReachabilitySwift

EFInternetIndicator Requirements Xcode 8.0+ iOS 8.3+ WARNING : It's not work on simulator. #1 Installation CocoaPods You can use CocoaPods to install

Ezequiel França 131 Dec 14, 2022
Private Internet Access - PIA VPN for iOS

Private Internet Access Private Internet Access is the world's leading consumer VPN service. At Private Internet Access we believe in unfettered acces

Private Internet Access - Free and Open Source Software 202 Dec 23, 2022
Simple asynchronous HTTP networking class for Swift

YYHRequest YYHRequest is a simple and lightweight class for loading asynchronous HTTP requests in Swift. Built on NSURLConnection and NSOperationQueue

yayuhh 77 May 18, 2022
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

Weni 10 Mar 28, 2022
Swift Express is a simple, yet unopinionated web application server written in Swift

Documentation <h5 align="right"><a href="http://demo.swiftexpress.io/">Live ?? server running Demo <img src="https://cdn0.iconfinder.com/data/icons/

Crossroad Labs 850 Dec 2, 2022
A simple GCD based HTTP client and server, written in 'pure' Swift

SwiftyHTTP Note: I'm probably not going to update this any further - If you need a Swift networking toolset for the server side, consider: Macro.swift

Always Right Institute 116 Aug 6, 2022
A simple HTTP server written in Swift

http4swift http4swift is a tiny HTTP server library for Nest-compatible applications. This project is unstable, and the API might be changed at anytim

Shun Takebayashi 27 Jun 29, 2022
Simple iOS app in Swift to show AQI for some cities using websocket using Combine + MVVM

AQI Simple iOS app in Swift to show AQI for some cities using websocket using Combine + MVVM This app follows MVVM This app uses combine framework The

Amey Vikkram Tiwari 2 Nov 6, 2022
Parsing Simple HTTP Headers for swift

HTTP Headers Parsing simple HTTP headers using pre-defined header descriptions. Examples: let response = HTTPURLRseponse(..., headers: [ "X-RateLi

Alexander Grebenyuk 12 May 17, 2022
Login-screen-UI - A simple iOS login screen written in Swift 5

This project has been updated to Swift 5 and Xcode 11.2 About This is a simple i

Kushal Shingote 2 Feb 4, 2022
SwiftyReachability is a simple and lightweight network interface manager written in Swift.

SwiftyReachability is a simple and lightweight network interface manager written in Swift. Freely inspired by https://github.com/tonymillion/Reachabil

Antonio Guerra 5 Nov 4, 2022
A simple OAuth library for iOS with a built-in set of providers

SwiftyOAuth is a small OAuth library with a built-in set of providers and a nice API to add your owns. let instagram: Provider = .instagram(clientID:

Damien 477 Oct 15, 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
Simple Wi-Fi analyzer for macOS

Wandra Simple Wi-Fi analyzer for macOS built in SwiftUI. It displays your Signal to Noise Ratio and Received Signal Strength. Basestation SSID and MAC

Mikael Löfgren 15 Dec 29, 2022
Showcasing simple SwiftUI and networking capabilities

CovidCounts CovidCounts is powered by SwiftUI. It allows a user to view COVID related data for different U.S. States. App Purpose This app is showcasi

Joel Sereno 1 Oct 15, 2021
QwikHttp is a robust, yet lightweight and simple to use HTTP networking library for iOS, tvOS and watchOS

QwikHttp is a robust, yet lightweight and simple to use HTTP networking library. It allows you to customize every aspect of your http requests within a single line of code, using a Builder style syntax to keep your code super clean.

Logan Sease 2 Mar 20, 2022