![](/img/trans.png)
[英]How to check for internet connectivity when device is connected to a network (wifi/3G) but there is no internet access in iOS using Swift?
[英]Swift: Check if iOS is connected to wifi without internet
我需要檢查iPhone是否已連接至無法提供互聯網連接的wifi網絡。 據我所知,Reachability等人正在檢查互聯網連接,這不是我所需要的。
我要連接的設備是沒有互聯網功能的專有設備。 我需要知道是否通過wifi連接到它。
// MARK: - Network Checker
func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}
var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
return false
}
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.