Created
April 30, 2018 10:25
-
-
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)
This file contains 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
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) | |
} | |
} | |
} |
@floriankrueger - Which Reachability framework does this script use ?
reachability.currentReachabilityStatus has been renamed to reachability.connect
I have updated it here:
https://gist.github.com/speedoholic/1746ac93be8e26723ce4023f0f4d211a
I've made an adaptaion of that gist for Alamofire. https://gist.github.com/abesmon/adf60da9acdca9394fa3be89d44dc5e8
'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
@floriankrueger - Which Reachability framework does this script use ?