Forked from floriankrueger/Reachability+NetworkType.swift
Last active
March 11, 2019 13:21
-
-
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)
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
// | |
// 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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I use this to determine if the phone is 4G compatible?