-1

I want to implement a functionality in the appdelegate which checks for internet connection whenever the app is running and show alerts accordingly.I wouldnt want to code in each and every class checking for internet connectivity. Is this possible? Iam using Alamofire and SwiftyJson too in the project.

  • you can use AlamofireNetworkActivityIndicator for this, [Here](http://stackoverflow.com/questions/41327325/how-to-check-internet-connection-in-alamofire/41327866#41327866) in an answer. – Umair Afzal Jan 18 '17 at 07:59

2 Answers2

1

Add this class in ur project:

import SystemConfiguration

open class Reachability {
class func isConnectedToNetwork() -> Bool {

    var zeroAddress = sockaddr_in()
    zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
    zeroAddress.sin_family = sa_family_t(AF_INET)

    let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
        $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
            SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
        }
    }

    var flags = SCNetworkReachabilityFlags()
    if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
        return false
    }

    let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
    let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
    return (isReachable && !needsConnection)
}
}

Use it where ever you want as follows :

if (Reachability.isConnectedToNetwork() == true){
       // Do what you want...
    }
    else{
        //Show alert
    }
Khadija Daruwala
  • 1,185
  • 3
  • 25
  • 54
0

You could make use of the SystemConfiguration framework (Determine the reachability of the device) to do so. Then make a swift file like the example below to keep things clean. This will handle all the cases. This will check for no internet, cellular and wifi.

Connection.swift

import SystemConfiguration


protocol Utilities {
}

extension NSObject:Utilities{


    enum ReachabilityStatus {
        case notReachable
        case reachableViaWWAN
        case reachableViaWiFi
    }

    var currentReachabilityStatus: ReachabilityStatus {

        var zeroAddress = sockaddr_in()
        zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
        zeroAddress.sin_family = sa_family_t(AF_INET)

        guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {
            $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
                SCNetworkReachabilityCreateWithAddress(nil, $0)
            }
        }) else {
            return .notReachable
        }

        var flags: SCNetworkReachabilityFlags = []
        if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
            return .notReachable
        }

        if flags.contains(.reachable) == false {
            // The target host is not reachable.
            return .notReachable
        }
        else if flags.contains(.isWWAN) == true {
            // WWAN connections are OK if the calling application is using the CFNetwork APIs.
            return .reachableViaWWAN
        }
        else if flags.contains(.connectionRequired) == false {
            // If the target host is reachable and no connection is required then we'll assume that you're on Wi-Fi...
            return .reachableViaWiFi
        }
        else if (flags.contains(.connectionOnDemand) == true || flags.contains(.connectionOnTraffic) == true) && flags.contains(.interventionRequired) == false {
            // The connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs and no [user] intervention is needed
            return .reachableViaWiFi
        }
        else {
            return .notReachable
        }
    }

}

Then you can use it however you like: Here is a sample to check if no internet.

if currentReachabilityStatus == .notReachable {
        // Internet is offline
        // show alert or do something.
    }
Cliffordwh
  • 1,422
  • 1
  • 10
  • 18