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.