How to use NetworkReachabilityManager in Alamofire
Asked Answered
F

11

33

I want functionality similar to AFNetworking in Objective-C with Alamofire NetworkReachabilityManager in Swift:

//Reachability detection
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    switch (status) {
        case AFNetworkReachabilityStatusReachableViaWWAN: {
            [self LoadNoInternetView:NO];
            break;
        }
        case AFNetworkReachabilityStatusReachableViaWiFi: {
            [self LoadNoInternetView:NO];
            break;
        }
        case AFNetworkReachabilityStatusNotReachable: {
            break;
        }
        default: {
            break;
        }
    }
}];

I am currently using the listener to know the status changes with network

let net = NetworkReachabilityManager()
net?.startListening()

Can someone describe how to support those use cases?

Fault answered 16/2, 2016 at 8:45 Comment(0)
F
19

I found the answer myself i.e by just writing a listener with closure as mentioned below:

let net = NetworkReachabilityManager()

net?.listener = { status in
    if net?.isReachable ?? false {

    switch status {

    case .reachable(.ethernetOrWiFi):
        print("The network is reachable over the WiFi connection")

    case .reachable(.wwan):
        print("The network is reachable over the WWAN connection")

    case .notReachable:
        print("The network is not reachable")

    case .unknown :
        print("It is unknown whether the network is reachable")

    }
}

net?.startListening()
Fault answered 16/2, 2016 at 12:13 Comment(1)
can you explain your answer further more? I mean exactly where do I have to add this code should I create the separate structure for this code?Recitative
A
39

NetworkManager Class

class NetworkManager {

//shared instance
static let shared = NetworkManager()

let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.google.com")

func startNetworkReachabilityObserver() {

    reachabilityManager?.listener = { status in
        switch status {

            case .notReachable:
                print("The network is not reachable")

            case .unknown :
                print("It is unknown whether the network is reachable")

            case .reachable(.ethernetOrWiFi):
                print("The network is reachable over the WiFi connection")

            case .reachable(.wwan):
                print("The network is reachable over the WWAN connection")

            }
        }

        // start listening
        reachabilityManager?.startListening()
   }
}

Start Network Reachability Observer

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // add network reachability observer on app start
        NetworkManager.shared.startNetworkReachabilityObserver()

        return true
    }
}
Araxes answered 29/9, 2016 at 9:38 Comment(4)
reachabilityManager?.startListening() you place at wrong place bro. :DReggi
I tried this but the listener gets called once when you turn off the wifi and not getting called back again when you turn it on. I tried holding a reference to it as I read in several places. For instance, I tried with a singleton but still experiencing this.Wrongful
@JesusAdolfoRodriguez - I had the exact same problem testing my app in the simulator, but when I installed my app on a device, the reachability manager worked as written here.Spatz
we found that using reachability against a host would give false unreachable under certain enterprise networks with proxy servers. I dont know exactly as the network at the time was setup by a third party, We decided simply to just start it with no host.Mistakable
F
19

I found the answer myself i.e by just writing a listener with closure as mentioned below:

let net = NetworkReachabilityManager()

net?.listener = { status in
    if net?.isReachable ?? false {

    switch status {

    case .reachable(.ethernetOrWiFi):
        print("The network is reachable over the WiFi connection")

    case .reachable(.wwan):
        print("The network is reachable over the WWAN connection")

    case .notReachable:
        print("The network is not reachable")

    case .unknown :
        print("It is unknown whether the network is reachable")

    }
}

net?.startListening()
Fault answered 16/2, 2016 at 12:13 Comment(1)
can you explain your answer further more? I mean exactly where do I have to add this code should I create the separate structure for this code?Recitative
X
16

Here's my implementation. I use it in a singleton. Remember to hold on to the reachability manager reference.

let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.apple.com")

func listenForReachability() {
    self.reachabilityManager?.listener = { status in
        print("Network Status Changed: \(status)")
        switch status {
        case .NotReachable:
            //Show error state
        case .Reachable(_), .Unknown:
            //Hide error state
        }
    }

    self.reachabilityManager?.startListening()
}
Xmas answered 18/5, 2016 at 18:52 Comment(0)
S
9

Using a singleton is working as I long as you keep a reference of reachabilityManager

class NetworkStatus {
static let sharedInstance = NetworkStatus()

private init() {}

let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.apple.com")

func startNetworkReachabilityObserver() {
    reachabilityManager?.listener = { status in

        switch status {

        case .notReachable:
            print("The network is not reachable")

        case .unknown :
            print("It is unknown whether the network is reachable")

        case .reachable(.ethernetOrWiFi):
            print("The network is reachable over the WiFi connection")

        case .reachable(.wwan):
            print("The network is reachable over the WWAN connection")

        }
    }
    reachabilityManager?.startListening()
}

So you can use it like this anywhere in your app:

let networkStatus = NetworkStatus.sharedInstance

override func awakeFromNib() {
    super.awakeFromNib()
    networkStatus.startNetworkReachabilityObserver()
}

You will be notified of any change in your network status. Just for icing on the cake this is a very good animation to show on your internet connection loss.

Slipcover answered 1/6, 2017 at 6:59 Comment(0)
R
9

SWIFT 5

NetworkState Structure

import Foundation
import Alamofire

struct NetworkState {

    var isInternetAvailable:Bool
    {
        return NetworkReachabilityManager()!.isReachable
    }
}

Use: -

  if (NetworkState().isInternetAvailable) {
        // Your code here
   }
Rustin answered 16/7, 2019 at 8:0 Comment(1)
Does this mean we no longer have to use reachabilityManager?.listener since it gives an error Value of type 'NetworkReachabilityManager' has no member 'listener'Galore
D
6

Swift 5: No need for listener object . Just we need to call the closure :

struct Network {

    let manager = Alamofire.NetworkReachabilityManager()

    func state() {
        manager?.startListening { status in
            switch status {
            case .notReachable :
                print("not reachable")
            case .reachable(.cellular) :
                print("cellular")
            case .reachable(.ethernetOrWiFi) :
                print("ethernetOrWiFi")
            default :
                print("unknown")
            } 
        }
    }
}

You can start using this function like :

Network().state()
Dorcasdorcea answered 24/2, 2020 at 9:48 Comment(0)
C
3

Apple says to use a struct instead of a class when you can. So here's my version of @rmooney and @Ammad 's answers, but using a struct instead of a class. Additionally, instead of using a method or function, I am using a computed property and I got that idea from this Medium post by @Abhimuralidharan. I'm just putting both the idea of using a struct instead of a class (so you don't have to have a singleton) and using a computed property instead of a method call together in one solution.

Here's the struct NetworkState:

import Foundation
import Alamofire

struct NetworkState {

    var isConnected: Bool {
        // isReachable checks for wwan, ethernet, and wifi, if
        // you only want 1 or 2 of these, the change the .isReachable
        // at the end to one of the other options.
        return NetworkReachabilityManager(host: www.apple.com)!.isReachable
    }
}

Here is how you use it in any of your code:

if NetworkState().isConnected {
    // do your is Connected stuff here
}
Cati answered 5/10, 2018 at 14:35 Comment(0)
A
3

To create NetworkManager Class as follows (For SWIFT 5)

import UIKit
import Alamofire
class NetworkManager {
    static let shared = NetworkManager()
    let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.apple.com")
    func startNetworkReachabilityObserver() {
        reachabilityManager?.startListening(onUpdatePerforming: { status in

            switch status {
                            case .notReachable:
                                print("The network is not reachable")
                            case .unknown :
                                print("It is unknown whether the network is reachable")
                            case .reachable(.ethernetOrWiFi):
                                print("The network is reachable over the WiFi connection")
                            case .reachable(.cellular):
                                print("The network is reachable over the cellular connection")
                      }
        })
    }
}

And the usage will be like

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // add network reachability observer on app start
        NetworkManager.shared.startNetworkReachabilityObserver()

        return true
    }
}
Aforetime answered 27/5, 2020 at 3:26 Comment(2)
- Alamofire (4.9.1): startListening is just returning boolObau
that's because this code is for compatible with Alamofire 5 which changed this part of the APIBonds
G
2

Alamofire 5 and above

import Alamofire

// MARK: NetworkReachability

final class NetworkReachability {
    
    static let shared = NetworkReachability()

    private let reachability = NetworkReachabilityManager(host: "www.apple.com")!

    typealias NetworkReachabilityStatus = NetworkReachabilityManager.NetworkReachabilityStatus

    private init() {}
    
    /// Start observing reachability changes
    func startListening() {
        reachability.startListening { [weak self] status in
            switch status {
            case .notReachable:
                self?.updateReachabilityStatus(.notReachable)
            case .reachable(let connection):
                self?.updateReachabilityStatus(.reachable(connection))
            case .unknown:
                break
            }
        }
    }
    
    /// Stop observing reachability changes
    func stopListening() {
        reachability.stopListening()
    }
    
    
    /// Updated ReachabilityStatus status based on connectivity status
    ///
    /// - Parameter status: `NetworkReachabilityStatus` enum containing reachability status
    private func updateReachabilityStatus(_ status: NetworkReachabilityStatus) {
        switch status {
        case .notReachable:
            print("Internet not available")
        case .reachable(.ethernetOrWiFi), .reachable(.cellular):
            print("Internet available")
        case .unknown:
            break
        }
    }

    /// returns current reachability status
    var isReachable: Bool {
        return reachability.isReachable
    }

    /// returns if connected via cellular
    var isConnectedViaCellular: Bool {
        return reachability.isReachableOnCellular
    }

    /// returns if connected via cellular
    var isConnectedViaWiFi: Bool {
        return reachability.isReachableOnEthernetOrWiFi
    }

    deinit {
        stopListening()
    }
}

How to use:

Call NetworkReachability.shared.startListening() from AppDelegate to start listening for reachability changes

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
            
    var window: UIWindow?
           
            
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
             
        NetworkReachability.shared.startListening()
        
        // window and rootviewcontroller setup code
        
        return true
    }
        
}
   
Gavage answered 10/8, 2020 at 14:16 Comment(0)
G
0

Just slight Improvement in the Alamofire 5

class NetworkManager {

//shared instance
static let shared = NetworkManager()

let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.google.com")

func startNetworkReachabilityObserver() {
    
    reachabilityManager?.startListening { status in
        switch status {

        case .notReachable:
            print("The network is not reachable")

        case .unknown :
            print("It is unknown whether the network is reachable")

        case .reachable(.ethernetOrWiFi):
            print("The network is reachable over the WiFi connection")

        case .reachable(.cellular):
            print("The network is reachable over the cellular connection")

        }
     }
  }

 }
Gignac answered 24/3, 2021 at 5:3 Comment(0)
P
-1

Solution for swift 4* + swift 5* and Alamofire 4.5+

CREATE a NetworkReachabilityManager class from Alamofire and configure the checkNetwork() method

import Alamofire

class Connectivity {   
    class func checkNetwork() ->Bool {
        return NetworkReachabilityManager()!.isReachable
    }
}

USAGE

switch Connectivity.checkNetwork() {
  case true:
      print("network available")
      //perform task
  case false:
      print("no network")
}
Plenipotentiary answered 14/5, 2020 at 4:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.