Created
February 12, 2018 09:43
-
-
Save iAmrSalman/0ef30fd4192372a50e4feeb3cf1f7607 to your computer and use it in GitHub Desktop.
[Reachability.swift] #swift #networkimg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Reachability.swift | |
// | |
// Created by Amr Salman on 4/4/17. | |
// | |
// | |
import Foundation | |
import SystemConfiguration | |
import UIKit | |
import SystemConfiguration.CaptiveNetwork | |
public let ReachabilityStatusChangedNotification = "ReachabilityStatusChangedNotification" | |
public enum ReachabilityType: CustomStringConvertible { | |
case WWAN | |
case WiFi | |
public var description: String { | |
switch self { | |
case .WWAN: return "WWAN" | |
case .WiFi: return "WiFi" | |
} | |
} | |
} | |
public enum ReachabilityStatus: CustomStringConvertible { | |
case Offline | |
case Online(ReachabilityType) | |
case Unknown | |
public var description: String { | |
switch self { | |
case .Offline: return "Offline" | |
case .Online(let type): return "Online (\(type))" | |
case .Unknown: return "Unknown" | |
} | |
} | |
} | |
public class Reachability { | |
public init() { } | |
public func connectionStatus() -> ReachabilityStatus { | |
var zeroAddress = sockaddr_in() | |
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress)) | |
zeroAddress.sin_family = sa_family_t(AF_INET) | |
guard let defaultRouteReachability = (withUnsafePointer(to: &zeroAddress) { | |
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) { zeroSockAddress in | |
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress) | |
} | |
}) else { | |
return .Unknown | |
} | |
var flags : SCNetworkReachabilityFlags = [] | |
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) { | |
return .Unknown | |
} | |
return ReachabilityStatus(reachabilityFlags: flags) | |
} | |
public func monitorReachabilityChanges() { | |
let host = "google.com" | |
var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil) | |
let reachability = SCNetworkReachabilityCreateWithName(nil, host)! | |
SCNetworkReachabilitySetCallback(reachability, { (_, flags, _) in | |
let status = ReachabilityStatus(reachabilityFlags: flags) | |
NotificationCenter.default.post(name: NSNotification.Name(rawValue: ReachabilityStatusChangedNotification), object: nil, userInfo: ["Status": status.description])}, &context) | |
SCNetworkReachabilityScheduleWithRunLoop(reachability, CFRunLoopGetMain(), CFRunLoopMode.commonModes.rawValue) | |
} | |
} | |
extension ReachabilityStatus { | |
public init(reachabilityFlags flags: SCNetworkReachabilityFlags) { | |
let connectionRequired = flags.contains(.connectionRequired) | |
let isReachable = flags.contains(.reachable) | |
let isWWAN = flags.contains(.isWWAN) | |
if !connectionRequired && isReachable { | |
if isWWAN { | |
self = .Online(.WWAN) | |
} else { | |
self = .Online(.WiFi) | |
} | |
} else { | |
self = .Offline | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment