当前位置:网站首页>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
}
}
}
}
边栏推荐
- The describeregion interface of CVM is special and has two versions
- Examples of corpus data processing cases (reading multiple text files, reading multiple files specified under a folder, decoding errors, reading multiple subfolder text, batch renaming of multiple fil
- Using kubeconfig files to organize cluster access
- 3-列表简介
- Review of postgraduate English final exam
- Case examples of corpus data processing (cases related to sentence retrieval)
- Exness: Powell insisted on his anti inflation commitment and pointed out that recession is possible
- Cloud development who is the source code of undercover applet
- 第 3 篇:绘制三角形
- Solution to the error of running NPM run eject
猜你喜欢

研究生英语期末考试复习

免费ICP域名备案查接口

开放合作,共赢未来 | 福昕鲲鹏加入金兰组织

Vulnhub target: boredhackerblog_ CLOUD AV

Selenium IDE的安装以及使用
![[run the script framework in Django and store the data in the database]](/img/6b/052679e5468e5a90be5c4339183f43.png)
[run the script framework in Django and store the data in the database]

Part 2: drawing a window

没有专业背景,还有机会成为机器学习工程师吗?

Keep one decimal place and two decimal places

SCM stm32f103rb, BLDC DC motor controller design, schematic diagram, source code and circuit scheme
随机推荐
LeetCode练习——跳跃游戏、组合求和
不止于观测|阿里云可观测套件正式发布
First acquaintance with JUC - day01
4-operation list (loop structure)
3-list introduction
运行npm run eject报错解决方法
Notes on the use of date and time base
Chapter 1 overview of canvas
【资料上新】迅为基于3568开发板的NPU开发资料全面升级
IndexError: Target 7 is out of bounds.
Exness: Powell insisted on his anti inflation commitment and pointed out that recession is possible
Teach you how to use the reflect package to parse the structure of go - step 2: structure member traversal
4-操作列表(循环结构)
第 2 篇:繪制一個窗口
Chapter 2 line graph of canvas
3-列表简介
Gossip: what happened to 3aC?
Part 1: building OpenGL environment
GraphMAE----論文快速閱讀
uniapp uni-app H5 端如何取消 返回按钮的显示 autoBackButton不生效