我正在开发一个用Swift编写的iOS应用程序,该应用程序可与本地网络上的HTTP服务器通信,并且我正在使用Apple的Reachability类来确定运行HTTP服务器的远程计算机是否在线。这是代码:
...
let RemoteHost: String = "192.168.178.130"
var RemoteReachability: Reachability! = nil
var RemoteIsReachable: Bool = false
init() {
super.init()
self.RemoteReachability = Reachability(hostName: self.RemoteHost)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: self.RemoteReachability)
self.RemoteReachability.startNotifier()
self.RemoteIsReachable = (self.RemoteReachability.currentReachabilityStatus().value == ReachableViaWiFi.value)
}
func reachabilityChanged(notification: NSNotification) {
let ReachabilityInst: Reachability = notification.object as Reachability
self.RemoteIsReachable = (ReachabilityInst.currentReachabilityStatus().value == ReachableViaWiFi.value)
}
问题是无论远程计算机是联机还是脱机,
(ReachabilityInst.currentReachabilityStatus().value == ReachableViaWiFi.value)
只要我连接到Wifi网络,总是如此。但是,当我关闭Wifi时,它会导致错误而不是真实。我在这里做错什么了吗,还是Reachability类与Swift /
xCode 6 Beta不兼容?我也尝试过这个:
(ReachabilityInst.currentReachabilityStatus() == ReachableViaWiFi)
但这导致xCode告诉我“无法为接受提供的参数的’==’找到重载”,即使这两个参数看起来都属于’NetworkStatus’类型。
提前致谢。