我在我的快速项目中使用Reachability.我在AppDelegate中有以下代码
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
reachability.startNotifier()
它打电话
func reachabilityChanged(note: NSNotification) {
}
但我的问题是,正在调用所有请求.也就是说,我正在从服务器加载图像,所以当网络可达性发生变化时,这是方法获取所有请求的调用.我希望这个方法只能被调用一次.
我还尝试在ViewController中添加此通知和方法,但它不起作用.
任何帮助将不胜感激.提前致谢
最佳答案 您可以添加标志以防止执行代码,除非状态已更改如下:
var connectionState = "Connected"
let connectedState = "Connected"
let notConnectedState = "notConnected"
func checkForReachability(notification:NSNotification)
{
let networkReachability = notification.object as! Reachability;
var remoteHostStatus = networkReachability.currentReachabilityStatus()
if remoteHostStatus.value == NotReachable.value && connectedState == connectedState {
connectionState = notConnectedState
println("State Changed Not Connected")
} else if remoteHostStatus.value == ReachableViaWiFi.value && connectedState == notConnectedState {
connectionState = connectedState
println("State Changed Connected")
}
}