5

I need some help understanding why I'm seeing the following.

AFNetworking 3.0.4 on iOS9.x

If I grab the reachability manager with

AFNetworkReachabilityManager.sharedManager()

and then add a setReachabilityStatusChangeBlock then I get exactly what I expect.

  • Airplane mode: NotReachable
  • WiFi off: ReachableViaWWAN
  • WiFi on: ReachableViaWiFi

So - the general case all is working as expected.

But I'd like to be more precise - we know what IP address we're talking to so I'd like to use managerForAddress instead. Here's the code I've cargo-culted from the net (most examples out there are for obj-c not swift) - in the following - ip is a string of the address (dotted quad form):

let isLittleEndian = Int(OSHostByteOrder()) == OSLittleEndian

let htons  = isLittleEndian ? _OSSwapInt16 : { $0 }

var address = sockaddr_in()
address.sin_len = UInt8(sizeofValue(address))
address.sin_family = sa_family_t(AF_INET)
address.sin_port = htons(443);
address.sin_addr.s_addr = inet_addr(ip);

addressReachability = withUnsafePointer(&address) {
    AFNetworkReachabilityManager(forAddress: UnsafePointer($0))
}

I can then add a setReachabilityStatusChangeBlock to this manager and start monitoring.

Now - it doesn't matter what I send in as IP - an empty string, a valid IP address where 443 is listening, a valid IP address where 443 is not listening, an invalid IP address - the results match exactly that of the sharedManager - if I'm on wifi - then it's ReachableViaWiFi, if I'm only on 4G then it's ReachableViaWWAN, if I'm in airplane mode it's NotReachable.

So - either I've totally misunderstood how managerForAddress is used or I have a code bug :) In either case I'd love to hear what I've misunderstood/got wrong.

  • If a non-AFNetworking Swift solution works for you, try http://github.com/ashleymills/reachability.swift. Take a look at the `init(hostname:)` method – Ashley Mills Feb 12 '16 at 10:26
  • I'll have a look. But - initial checking - surely I'd need to be using something that calls `SCNetworkReachabilityCreateWithAddress` rather than `SCNetworkReachabilityCreateWithName` ? - in the scenario I'm working with I only have IP addresses to work with. – Chris Searle Feb 18 '16 at 07:54
  • Further testing - hostname based reachability works (both with AFN and also SCN). But IP based still just copies the general sharedManager() result (also both with AFN and SCN). But I only have an IP address to work with - so I can't use hostname based – Chris Searle Feb 18 '16 at 08:45
  • @ChrisSearle can you test the pod [AppleReachability](https://cocoapods.org/pods/AppleReachability) which is a replica of Apple example https://developer.apple.com/library/content/samplecode/Reachability/ ? – Cœur Dec 15 '16 at 19:00

0 Answers0