当前位置:网站首页>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
}
}
}
}
边栏推荐
- Signature analysis of app x-zse-96 in a Q & a community
- 1-4metasploitable2介绍
- Thread considerations
- ImportError: cannot import name ‘process_pdf‘ from ‘pdfminer.pdfinterp‘错误完全解决
- Hilbert Huang Transform
- auto使用示例
- Common array encapsulation
- Notes on the use of date and time base
- Basics of reptile B1 - scrapy (learning notes of station B)
- Part 1: building OpenGL environment
猜你喜欢

Open cooperation and win-win future | Fuxin Kunpeng joins Jinlan organization

第 2 篇:繪制一個窗口

Backup and restore SQL Server Databases locally

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

GPU is not used when the code is running

Oracle-高级SQL限定查询

Case examples of corpus data processing (cases related to sentence retrieval)

Hilbert Huang Transform

研究生英语期末考试复习

The two most frequently asked locks in the interview
随机推荐
Decltype usage introduction
热赛道上的冷思考:乘数效应才是东数西算的根本要求
Gossip: what happened to 3aC?
5-if语句(选择结构)
Ad-gcl:advantageous graph augmentation to improve graph contractual learning
redolog和binlog
Specify IP when calling feign interface
Thread considerations
Case examples of corpus data processing (cases related to sentence retrieval)
3-列表简介
用Ngrok 配置属于自己的免费外网域名
报错“Computation failed in `stat_summary_hex()`”
Screenshot recommendation - snipaste
Solution to the error of running NPM run eject
[测试开发]初识软件测试
1-4metaploitable2 introduction
本地备份和还原 SQL Server 数据库
Configure your own free Internet domain name with ngrok
The two most frequently asked locks in the interview
关于h5页面苹果手机使用fixed定位tabbar最底部时遮挡内容问题