0

I need to check if an iPhone is connected to a wifi network that won't provide an internet connection. As far as I can see, Reachability et al check for an internet connection, which is not what I need.

The device I am connecting to is a proprietary device that has no internet functionality. I need to know whether I am connected to it or not via wifi.

HenryRootTwo
  • 2,572
  • 1
  • 27
  • 27
  • 1
    Just so we're not covering old ground, can you confirm if you've tried anything from [ios reachability wifi no internet](https://www.google.com/search?client=safari&rls=en&q=ios+reachability+wifi+no+internet&ie=UTF-8&oe=UTF-8), including [Reachability not working when wi-fi connected but no internet](https://stackoverflow.com/questions/13411606/reachability-not-working-when-wi-fi-connected-but-no-internet) and/or [Handling internet connection reachability in Swift](https://blog.pusher.com/handling-internet-connection-reachability-swift/)? – MadProgrammer Sep 05 '18 at 22:29
  • Does you device have an IP or domain name? I'd check if that's reachable, if so, it's probably a reasonable guess about the state (and yes, I've done this too) – MadProgrammer Sep 05 '18 at 22:43

1 Answers1

1
// 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)
}
Bola Ibrahim
  • 720
  • 9
  • 8