如何在 swift 2.2 中检查 3G、4G 和 wifi 互联网连接
How to check 3G,4G and wifi internet connection in swift 2.2
导入基金会
导入系统配置
public class 可达性 {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer([=11=]))
}
var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
return false
}
let isReachable = flags == .Reachable
let needsConnection = flags == .ConnectionRequired
return isReachable && !needsConnection
}
}
我正在使用上面编写的代码来检查我的应用程序的互联网连接,这只检查 3G 和 WIFI 连接。但我还需要检查 4G 连接。谁能帮我找到解决办法。
这里是Apple的Reachability,你需要下载并拖拽Reachability.h/.m到你的项目中。
然后 import CoreTelephony
并在下面尝试。
if let reachability = Reachability.forInternetConnection() {
reachability.startNotifier()
let status = reachability.currentReachabilityStatus()
if status == .init(0) {
// .NotReachable
print("Not Reachable")
}
else if status == .init(1) {
// .ReachableViaWiFi
print("Reachable Via WiFi")
}
else if status == .init(2) {
// .ReachableViaWWAN
let netInfo = CTTelephonyNetworkInfo()
if let cRAT = netInfo.currentRadioAccessTechnology {
switch cRAT {
case CTRadioAccessTechnologyGPRS,
CTRadioAccessTechnologyEdge,
CTRadioAccessTechnologyCDMA1x:
print("Reachable Via 2G")
case CTRadioAccessTechnologyWCDMA,
CTRadioAccessTechnologyHSDPA,
CTRadioAccessTechnologyHSUPA,
CTRadioAccessTechnologyCDMAEVDORev0,
CTRadioAccessTechnologyCDMAEVDORevA,
CTRadioAccessTechnologyCDMAEVDORevB,
CTRadioAccessTechnologyeHRPD:
print("Reachable Via 3G")
case CTRadioAccessTechnologyLTE:
print("Reachable Via 4G")
default:
fatalError("error")
}
}
}
}
最新可达性swift 工作代码。
if let status = appReachablity?.currentReachabilityStatus {
var message = ""
if status == .notReachable {
// .NotReachable
message = "DOT##=> Not Reachable"
print(message)
}
else if status == .reachableViaWiFi {
// .ReachableViaWiFi
message = "DOT##=> Reachable Via WiFi"
print(message)
}
else if status == .reachableViaWWAN {
// .ReachableViaWWAN
let netInfo = CTTelephonyNetworkInfo()
if let cRAT = netInfo.currentRadioAccessTechnology {
switch cRAT {
case CTRadioAccessTechnologyGPRS,
CTRadioAccessTechnologyEdge,
CTRadioAccessTechnologyCDMA1x:
message = "DOT##=> Reachable Via 2G"
print(message)
case CTRadioAccessTechnologyWCDMA,
CTRadioAccessTechnologyHSDPA,
CTRadioAccessTechnologyHSUPA,
CTRadioAccessTechnologyCDMAEVDORev0,
CTRadioAccessTechnologyCDMAEVDORevA,
CTRadioAccessTechnologyCDMAEVDORevB,
CTRadioAccessTechnologyeHRPD:
message = "DOT##=> Reachable Via 3G"
print(message)
case CTRadioAccessTechnologyLTE:
message = "DOT##=> Reachable Via 4G"
print(message)
default:
fatalError("error")
}
}
}
Toast(text: message, duration: Delay.long).show()
}
导入基金会 导入系统配置
public class 可达性 {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer([=11=]))
}
var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
return false
}
let isReachable = flags == .Reachable
let needsConnection = flags == .ConnectionRequired
return isReachable && !needsConnection
}
}
我正在使用上面编写的代码来检查我的应用程序的互联网连接,这只检查 3G 和 WIFI 连接。但我还需要检查 4G 连接。谁能帮我找到解决办法。
这里是Apple的Reachability,你需要下载并拖拽Reachability.h/.m到你的项目中。
然后 import CoreTelephony
并在下面尝试。
if let reachability = Reachability.forInternetConnection() {
reachability.startNotifier()
let status = reachability.currentReachabilityStatus()
if status == .init(0) {
// .NotReachable
print("Not Reachable")
}
else if status == .init(1) {
// .ReachableViaWiFi
print("Reachable Via WiFi")
}
else if status == .init(2) {
// .ReachableViaWWAN
let netInfo = CTTelephonyNetworkInfo()
if let cRAT = netInfo.currentRadioAccessTechnology {
switch cRAT {
case CTRadioAccessTechnologyGPRS,
CTRadioAccessTechnologyEdge,
CTRadioAccessTechnologyCDMA1x:
print("Reachable Via 2G")
case CTRadioAccessTechnologyWCDMA,
CTRadioAccessTechnologyHSDPA,
CTRadioAccessTechnologyHSUPA,
CTRadioAccessTechnologyCDMAEVDORev0,
CTRadioAccessTechnologyCDMAEVDORevA,
CTRadioAccessTechnologyCDMAEVDORevB,
CTRadioAccessTechnologyeHRPD:
print("Reachable Via 3G")
case CTRadioAccessTechnologyLTE:
print("Reachable Via 4G")
default:
fatalError("error")
}
}
}
}
最新可达性swift 工作代码。
if let status = appReachablity?.currentReachabilityStatus {
var message = ""
if status == .notReachable {
// .NotReachable
message = "DOT##=> Not Reachable"
print(message)
}
else if status == .reachableViaWiFi {
// .ReachableViaWiFi
message = "DOT##=> Reachable Via WiFi"
print(message)
}
else if status == .reachableViaWWAN {
// .ReachableViaWWAN
let netInfo = CTTelephonyNetworkInfo()
if let cRAT = netInfo.currentRadioAccessTechnology {
switch cRAT {
case CTRadioAccessTechnologyGPRS,
CTRadioAccessTechnologyEdge,
CTRadioAccessTechnologyCDMA1x:
message = "DOT##=> Reachable Via 2G"
print(message)
case CTRadioAccessTechnologyWCDMA,
CTRadioAccessTechnologyHSDPA,
CTRadioAccessTechnologyHSUPA,
CTRadioAccessTechnologyCDMAEVDORev0,
CTRadioAccessTechnologyCDMAEVDORevA,
CTRadioAccessTechnologyCDMAEVDORevB,
CTRadioAccessTechnologyeHRPD:
message = "DOT##=> Reachable Via 3G"
print(message)
case CTRadioAccessTechnologyLTE:
message = "DOT##=> Reachable Via 4G"
print(message)
default:
fatalError("error")
}
}
}
Toast(text: message, duration: Delay.long).show()
}