Skip to content

Instantly share code, notes, and snippets.

@floriankrueger
Created April 30, 2018 10:25
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save floriankrueger/46e6f9f4c910b5313a72b358fdad25cc to your computer and use it in GitHub Desktop.
Save floriankrueger/46e6f9f4c910b5313a72b358fdad25cc to your computer and use it in GitHub Desktop.
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
Copy link

@floriankrueger - Which Reachability framework does this script use ?

@speedoholic
Copy link

@floriankrueger - Which Reachability framework does this script use ?

https://cocoapods.org/pods/ReachabilitySwift

@speedoholic
Copy link

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

@abesmon
Copy link

abesmon commented May 20, 2019

I've made an adaptaion of that gist for Alamofire. https://gist.github.com/abesmon/adf60da9acdca9394fa3be89d44dc5e8

@SlavcoPetkovski
Copy link

'currentRadioAccessTechnology' was deprecated in iOS 12.0.
Any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment