当前位置:网站首页>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
}
}
}
}
边栏推荐
- Ad-gcl:advantageous graph augmentation to improve graph contractual learning
- Screenshot recommendation - snipaste
- 火线,零线,地线,你知道这三根线的作用是什么吗?
- JDBC 在性能测试中的应用
- GPU is not used when the code is running
- 调用Feign接口时指定ip
- . No main manifest attribute in jar
- Baidu map, coordinate inversion, picking coordinate position
- Svn actual measurement common operation record operation
- Open cooperation and win-win future | Fuxin Kunpeng joins Jinlan organization
猜你喜欢

Hilbert Huang Transform

Hongmeng OS development III

Oracle advanced SQL qualified query

ImportError: cannot import name ‘process_ pdf‘ from ‘pdfminer. Pdfinterp 'error completely resolved

Do you still have the opportunity to become a machine learning engineer without professional background?

GPU is not used when the code is running

基于Distiller的模型压缩工具简介

宝塔面板安装php7.2安装phalcon3.3.2

Ad-gcl:advantageous graph augmentation to improve graph contractual learning
![[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]
随机推荐
从 jsonpath 和 xpath 到 SPL
不止于观测|阿里云可观测套件正式发布
单片机STM32F103RB,BLDC直流电机控制器设计,原理图、源码和电路方案
鸿蒙os开发三
Configure your own free Internet domain name with ngrok
解决笔记本键盘禁用失败问题
Cold thinking on the hot track: multiplier effect is the fundamental requirement of East West calculation
『C语言』系统日期&时间
Take my brother to make a real-time Leaderboard
Hongmeng OS development III
[C language] system date & time
第 2 篇:繪制一個窗口
Thread considerations
Echart's experience (I): about y axis yaxis attribute
The two most frequently asked locks in the interview
Screenshot recommendation - snipaste
GraphMAE----論文快速閱讀
ImportError: cannot import name ‘process_ pdf‘ from ‘pdfminer. Pdfinterp 'error completely resolved
Los Angeles p1051 who won the most Scholarships
研究生英语期末考试复习