When my application re-enters the foreground, my SCNetworkReachabilityCallBack
is called with an empty flags object (i.e. without the reachable
flag) which tells me that the device can no longer reach the asset required...
At first I thought this could simply be because the app entered the background and at that point perhaps a non-reachable notification was sent which is delivered when the app enters the foreground, but there is no subsequent reachable
callback to the function so this can't be the case.
Is anyone else experiencing this issue, or have any ideas why it might be happening? I'm using the reachability class to show UI, so this is quite key to my application!
Code to setup queue is:
// Creates a context
var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
// Sets `self` as listener object
context.info = UnsafeMutableRawPointer(Unmanaged<Reachability>.passUnretained(self).toOpaque())
let callbackClosure: SCNetworkReachabilityCallBack? = {
(reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutableRawPointer?) in
guard let info = info else { return }
// Gets the `Handler` object from the context info
let handler = Unmanaged<Reachability>.fromOpaque(info).takeUnretainedValue()
handler.queue.async {
handler.checkReachability(flags: flags)
}
}
// Registers the callback. `callbackClosure` is the closure where we manage the callback implementation
if !SCNetworkReachabilitySetCallback(reachability, callbackClosure, &context) {
// Not able to set the callback
}
SCNetworkReachability
object is setup in the init method:
init?(hostName: String) {
guard let _reachability = SCNetworkReachabilityCreateWithName(nil, hostName) else { return nil }
reachability = _reachability
}