ios swift 有网络连接回调吗? [英] are there network connectivity callbacks for ios swift?

查看:59
本文介绍了ios swift 有网络连接回调吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在没有网络连接时阻止我的应用程序的用户界面.

I want to block my app's ui when there is no network connectivity.

我看到了有关检查网络连接的帖子.

I saw this post about checking network connectivity.

有没有办法用回调"onNetworkLost()","onNetworkAvailable()"注册监听器?而不是创建我自己的周期性任务?

Is there a way to register for a listener with callback "onNetworkLost()", "onNetworkAvailable()"? Instead of creating my own periodic task?

推荐答案

您可以使用 Reachability 类在网络状态发生变化时获得通知.请参阅 https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html 提供有关集成 Reachability 的文档和示例代码.它为您提供 kReachabilityChangedNotification 通知,该通知在网络状态改变时广播.收到通知后,您可以检查网络是否可用.

You can use Reachability class to get notification when network status has changed. Refer https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html for documentation as well as sample code on integrating Reachability. It gives you kReachabilityChangedNotification notification which is broadcasted when network status has changed. Once you get the notification you can check whether network is available or not.

你也可以参考这个如何使用可达性类来检测有效的互联网连接?

这篇关于ios swift 有网络连接回调吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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