当前位置:网站首页>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
}
}
}
}
边栏推荐
- Redolog and binlog
- 运行npm run eject报错解决方法
- 语料库数据处理个案实例(句子检索相关个案)
- SCM stm32f103rb, BLDC DC motor controller design, schematic diagram, source code and circuit scheme
- Graphmae ---- quick reading of papers
- Configure your own free Internet domain name with ngrok
- Using kubeconfig files to organize cluster access
- 1-4metaploitable2 introduction
- Jenkins 太老了 试试它?云原生 CI/CD Tekton
- 某问答社区App x-zse-96签名分析
猜你喜欢

Echart's experience (I): about y axis yaxis attribute

Gossip: what happened to 3aC?

Backup and restore SQL Server Databases locally

First acquaintance with JUC - day01

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

调用Feign接口时指定ip

Oracle advanced SQL qualified query

1-4metasploitable2介绍

从 jsonpath 和 xpath 到 SPL
![[nilm] non intrusive load decomposition module nilmtk installation tutorial](/img/d0/bc5ea1cbca9ee96a2fe168484ffec4.png)
[nilm] non intrusive load decomposition module nilmtk installation tutorial
随机推荐
『C语言』系统日期&时间
Auto usage example
Mousse shares listed on Shenzhen Stock Exchange: gross profit margin continued to decline, and marketing failed in the first quarter of 2022
Case examples of corpus data processing (cases related to sentence retrieval)
快速读论文----AD-GCL:Adversarial Graph Augmentation to Improve Graph Contrastive Learning
Review of postgraduate English final exam
Cold thinking on the hot track: multiplier effect is the fundamental requirement of East West calculation
redolog和binlog
OpenGauss数据库在 CentOS 上的实践,配置篇
4-操作列表(循环结构)
4-operation list (loop structure)
[test development] first knowledge of software testing
Part 2: drawing a window
Jenkins 太老了 试试它?云原生 CI/CD Tekton
Domain environment importing Tencent cloud considerations
Experience of Shenzhou computer
GraphMAE----論文快速閱讀
SVN实测常用操作-记录操作大全
Jenkins is too old try it? Cloud native ci/cd Tekton
ImportError: cannot import name ‘process_pdf‘ from ‘pdfminer.pdfinterp‘错误完全解决