Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save speedoholic/1746ac93be8e26723ce4023f0f4d211a to your computer and use it in GitHub Desktop.
Save speedoholic/1746ac93be8e26723ce4023f0f4d211a 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)
//
// ReachabilityHelper.swift
// TradeX
//
// Created by Kushal Ashok on 1/11/19.
//
import Reachability //https://cocoapods.org/pods/ReachabilitySwift
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)\""
}
}
var networkTypeInt: Int {
switch self {
case .unknown: return 9
case .noConnection: return 9
case .wifi: return 1
case .wwan2g: return 2
case .wwan3g: return 3
case .wwan4g: return 4
case .unknownTechnology: return 9
}
}
}
extension Reachability {
static func getNetworkType() -> NetworkType {
guard let reachability: Reachability = Reachability() else { return .unknown }
do {
try reachability.startNotifier()
switch reachability.connection {
case .none: return .noConnection
case .wifi: return .wifi
case .cellular: 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)
}
}
}
@chevi99
Copy link

chevi99 commented Jan 22, 2019

How can I use this to determine if the phone is 4G compatible?

@harshvyas7702
Copy link

How can I use this in swift 4?

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