使用Apple的Reachability检查Swift中的远程服务器可访问性 [英] Using Apple's Reachability to check remote server reachability in Swift

查看:124
本文介绍了使用Apple的Reachability检查Swift中的远程服务器可访问性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用Swift编写的iOS应用程序,它与本地网络上的HTTP服务器通信,我正在使用Apple的Reachability类来确定运行HTTP服务器的远程机器是否在线。这是代码:

I'm developing an iOS application written in Swift that communicates with a HTTP server on the local network, and I'm using Apple's Reachability class to determine wether the remote machine running the HTTP server is online or not. Here's the code:

...
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)
}

问题是无论远程机器是在线还是离线,

The problem is that no matter if the remote machine is online or offline,

(ReachabilityInst.currentReachabilityStatus().value == ReachableViaWiFi.value)

只要我连接到Wifi网络,总是如此。但是,当我关闭Wifi时,它会导致错误而不是真实。我在这里做错了什么,或者Reachability类是否与Swift / xCode 6 Beta不兼容?我也试过这个:

Is always true, as long as I'm connected to a Wifi network. However, when I turn Wifi off, it results in being false instead of true. Am I doing something wrong here, or is the Reachability class just not compatible with Swift/xCode 6 Beta yet? I've also tried this:

(ReachabilityInst.currentReachabilityStatus() == ReachableViaWiFi)

但是这导致xCode告诉我无法找到接受所提供参数的'=='的重载,即使两者看起来都是属于'NetworkStatus'类型。

But that results in xCode telling me "Could not find an overload for '==' that accepts the supplied arguments", even though both appear to be of type 'NetworkStatus'.

提前致谢。

推荐答案

您正在使用的Reachability类基于Apple的SCNetworkReachability类,它并不完全符合您的希望。来自 SCNetworkReachability文档

The Reachability class you're using is based on Apple's SCNetworkReachability class, which doesn't do exactly what you're hoping. From the SCNetworkReachability documentation:



应用程序发送到网络堆栈的数据包可以离开本地设备时,可以认为远程主机是可达的。
可达性不保证数据包实际上是主机收到的

A remote host is considered reachable when a data packet, sent by an application into the network stack, can leave the local device. Reachability does not guarantee that the data packet will actually be received by the host.

所以它不是用于测试远程主机是否实际在线,只是(1)当前网络设置是否允许尝试到达它和(2)通过什么方法。一旦确定网络处于活动状态,您就需要尝试连接以查看远程主机是否实际启动并运行。

So it's not built for testing whether or not the remote host is actually online, just whether (1) the current network settings will allow an attempt to reach it and (2) by what methods. Once you've determined that the network is active you'll need to make an attempt to connect to see if the remote host is actually up and running.

注意:此测试:

(ReachabilityInst.currentReachabilityStatus().value == ReachableViaWiFi.value)

是正确的检查方式 - 由于某种原因 NetworkStatus 是为数不多的Apple之一在没有NS_ENUM宏的情况下创建的枚举

is the correct way to check -- for some reason NetworkStatus is one of the few Apple enumerations created without the NS_ENUM macro.

这篇关于使用Apple的Reachability检查Swift中的远程服务器可访问性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆