Skip to content

Instantly share code, notes, and snippets.

@abesmon
Created May 20, 2019 11:23
Show Gist options
  • Save abesmon/adf60da9acdca9394fa3be89d44dc5e8 to your computer and use it in GitHub Desktop.
Save abesmon/adf60da9acdca9394fa3be89d44dc5e8 to your computer and use it in GitHub Desktop.
import Alamofire
import CoreTelephony
enum NetworkType {
case unknown
case noConnection
case wifi
case wwan2g
case wwan3g
case wwan4g
case unknownTechnology(name: String)
var trackingId: String {
switch self {
case .unknown: return "Unknown"
case .noConnection: return "No Connection"
case .wifi: return "Wifi"
case .wwan2g: return "2G"
case .wwan3g: return "3G"
case .wwan4g: return "4G"
case .unknownTechnology(let name): return "Unknown Technology: \"\(name)\""
}
}
}
extension NetworkReachabilityManager {
static func getNetworkType() -> NetworkType {
guard let reachability = NetworkReachabilityManager() else { return .unknown }
switch reachability.networkReachabilityStatus {
case .reachable(let connectionType):
switch connectionType {
case .ethernetOrWiFi:
return .wifi
case .wwan:
return getWWANNetworkType()
}
case .notReachable:
return .noConnection
case .unknown:
return .unknown
}
}
static func getWWANNetworkType() -> NetworkType {
guard let currentRadioAccessTechnology = CTTelephonyNetworkInfo().currentRadioAccessTechnology else { return .unknown }
switch currentRadioAccessTechnology {
case CTRadioAccessTechnologyGPRS,
CTRadioAccessTechnologyEdge,
CTRadioAccessTechnologyCDMA1x:
return .wwan2g
case CTRadioAccessTechnologyWCDMA,
CTRadioAccessTechnologyHSDPA,
CTRadioAccessTechnologyHSUPA,
CTRadioAccessTechnologyCDMAEVDORev0,
CTRadioAccessTechnologyCDMAEVDORevA,
CTRadioAccessTechnologyCDMAEVDORevB,
CTRadioAccessTechnologyeHRPD:
return .wwan3g
case CTRadioAccessTechnologyLTE:
return .wwan4g
default:
return .unknownTechnology(name: currentRadioAccessTechnology)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment