当我的应用程序重新进入前台时,我的SCNetworkReachabilityCallBack
被调用,并带有一个空标志对象(即没有reachable
标志),这告诉我设备无法再达到所需的资产。 ..
起初我以为这可能是因为应用程序进入了后台,这时也许发送了一个不可访问的通知,该通知在应用程序进入前台时传递,但是没有后续的reachable
回调功能,所以情况并非如此。
是否还有其他人遇到此问题,或者对为什么会发生此事有任何想法?我正在使用可达性类来显示UI,所以这对我的应用程序来说很关键!
设置队列的代码为:
// 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
对象是通过init方法设置的:
init?(hostName: String) {
guard let _reachability = SCNetworkReachabilityCreateWithName(nil, hostName) else { return nil }
reachability = _reachability
}