Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Getting the current network connection type through Reachability and CoreTelephony (based on https://stackoverflow.com/a/36451194/766873)
import Reachability
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 Reachability {
static func getNetworkType() -> NetworkType {
guard let reachability: Reachability = Reachability() else { return .unknown }
do {
try reachability.startNotifier()
switch reachability.currentReachabilityStatus {
case .notReachable: return .noConnection
case .reachableViaWiFi: return .wifi
case .reachableViaWWAN: return Reachability.getWWANNetworkType()
}
} catch {
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)
}
}
}
@gprasant

This comment has been minimized.

Copy link

commented Sep 17, 2018

@floriankrueger - Which Reachability framework does this script use ?

@speedoholic

This comment has been minimized.

Copy link

commented Jan 11, 2019

@floriankrueger - Which Reachability framework does this script use ?

https://cocoapods.org/pods/ReachabilitySwift

@speedoholic

This comment has been minimized.

Copy link

commented Jan 11, 2019

reachability.currentReachabilityStatus has been renamed to reachability.connect
I have updated it here:
https://gist.github.com/speedoholic/1746ac93be8e26723ce4023f0f4d211a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.