OSX/Swift:建立/恢复互联网连接后调用功能 [英] OSX/Swift: Call a function when internet connection is established/restored
问题描述
我想做的是在建立或恢复互联网连接后调用一些代码.
What I want to do is call some code when the internet connection is established or restored.
我知道有一些解决方案可以让我测试Internet连接是否处于活动状态,但是我想知道是否有一种方法可以通过Internet连接变为活动状态来触发功能.
I know there are solutions out there that let me test whether an internet connection is active or not, but I am wondering if there is a way for a function to be triggered by the internet connection becoming active.
如果没有,最好的解决方法是什么?每x秒检查一次连接?谢谢.
If not, what is the best workaround? Checking for a connection every x seconds? Thanks.
推荐答案
SCNetworkReachability API,以在IP地址或主机名的可达性状态更改时生成通知.创建一个SCNetworkReachabilityRef,然后设置一个回调,当检测到网络更改时将调用它.
The SCNetworkReachability APIs can be configured to generate notifications when the reachability status of an IP address or hostname changes. Create a SCNetworkReachabilityRef, then set a callback, and it will be called when a network change is detected.
这篇关于OSX/Swift:建立/恢复互联网连接后调用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!