当前位置:网站首页>Swift Extension NetworkUtil(网络监听)(源码)
Swift Extension NetworkUtil(网络监听)(源码)
2022-06-24 06:52:00 【冯汉栩】
一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。
前言
2022.06.23今天写了一个基于ios13及以上监听网络的封装。可以获取当前的网络状态,网络一发生变化就会收到通知
正题
使用起来非常方便
1.启动的时候开启网络监听
2.然后可以在想用的地方用了
import UIKit
import SnapKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
//获取网络状态的枚举汁
print("connectType: \(NetworkUtil.shared.connectType.rawValue)")
//获取网络状态的文本值
print("readableConnectType: \(NetworkUtil.shared.readableConnectType)")
//注册网络监听
NotificationCenter.default.addObserver(
self,
selector: #selector(onNetworkChange),
name: Notification.Name("onNetworkChange"),
object: nil
)
}
//网络一旦发生变化就会收到通知
@objc func onNetworkChange(notification : Notification){
DispatchQueue.main.async {
guard
let dictionary = notification.userInfo,
let type = dictionary["type"] as? Int
else {
return
}
let connectType = NetworkUtil.ConnectType(rawValue: type)!
switch (connectType) {
case .unknown:
print("目前无网络可用")
default:
break
}
}
}
}
边栏推荐
- Resolution error: LNK2019 unresolved external symbol
- 研究生英语期末考试复习
- What is the lifecycle of automated testing?
- 1-4metaploitable2 introduction
- How to cancel the display of the return button at the uniapp uni app H5 end the autobackbutton does not take effect
- 面试中的最常被问到的两种锁
- 站在风暴中心:如何给飞奔中的腾讯更换引擎
- Timer usage notes
- 力扣(LeetCode)174. 地下城游戏(2022.06.23)
- How does dating software cut your leeks
猜你喜欢

本地备份和还原 SQL Server 数据库

Selenium IDE的安装以及使用

Baidu map, coordinate inversion, picking coordinate position

What kind of experience is it when the Institute earns 20000 yuan a month!

解决错误: LNK2019 无法解析的外部符号

Introduction of model compression tool based on distiller

Cloud development who is the source code of undercover applet

Mousse shares listed on Shenzhen Stock Exchange: gross profit margin continued to decline, and marketing failed in the first quarter of 2022

SVN实测常用操作-记录操作大全

Graphmae - - lecture rapide des documents
随机推荐
Svn actual measurement common operation record operation
站在风暴中心:如何给飞奔中的腾讯更换引擎
Do you still have the opportunity to become a machine learning engineer without professional background?
软件工程导论——第三章——需求分析
Pair class notes
Vulnhub靶机:BOREDHACKERBLOG: SOCIAL NETWORK
Thread support
Los Angeles p1051 who won the most Scholarships
Practice of opengauss database on CentOS, configuration
某问答社区App x-zse-96签名分析
LeetCode练习——跳跃游戏、组合求和
JDBC 在性能测试中的应用
Leetcode 174 Dungeon games (June 23, 2022)
Graphmae - - lecture rapide des documents
Using kubeconfig files to organize cluster access
力扣(LeetCode)174. 地下城游戏(2022.06.23)
Signature analysis of app x-zse-96 in a Q & a community
Take my brother to make a real-time Leaderboard
.jar中没有主清单属性
面试中的最常被问到的两种锁