为了迅速,我们可以使用:
func getNetworkType()->String {
do{
let reachability:Reachability = try Reachability.reachabilityForInternetConnection()
do{
try reachability.startNotifier()
let status = reachability.currentReachabilityStatus
if(status == .NotReachable){
return ""
}else if (status == .ReachableViaWiFi){
return "Wifi"
}else if (status == .ReachableViaWWAN){
let networkInfo = CTTelephonyNetworkInfo()
let carrierType = networkInfo.currentRadioAccessTechnology
switch carrierType{
case CTRadioAccessTechnologyGPRS?,CTRadioAccessTechnologyEdge?,CTRadioAccessTechnologyCDMA1x?: return "2G"
case CTRadioAccessTechnologyWCDMA?,CTRadioAccessTechnologyHSDPA?,CTRadioAccessTechnologyHSUPA?,CTRadioAccessTechnologyCDMAEVDORev0?,CTRadioAccessTechnologyCDMAEVDORevA?,CTRadioAccessTechnologyCDMAEVDORevB?,CTRadioAccessTechnologyeHRPD?: return "3G"
case CTRadioAccessTechnologyLTE?: return "4G"
default: return ""
}
// Get carrier name
}else{
return ""
}
}catch{
return ""
}
}catch{
return ""
}
}