当前位置:网站首页>Swift Extension NetworkUtil(網絡監聽)(源碼)
Swift Extension NetworkUtil(網絡監聽)(源碼)
2022-06-24 08:05: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
}
}
}
}
边栏推荐
- 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
- Cold thinking on the hot track: multiplier effect is the fundamental requirement of East West calculation
- Hongmeng development IV
- 某问答社区App x-zse-96签名分析
- What is the lifecycle of automated testing?
- Moonwell Artemis is now online moonbeam network
- Jenkins is too old try it? Cloud native ci/cd Tekton
- 本地备份和还原 SQL Server 数据库
- POM configuration provided and test
- Exness: Powell insisted on his anti inflation commitment and pointed out that recession is possible
猜你喜欢

Free ICP domain name filing interface

Vulnhub靶机:BOREDHACKERBLOG_ CLOUD AV

免费ICP域名备案查接口

ImportError: cannot import name ‘process_pdf‘ from ‘pdfminer.pdfinterp‘错误完全解决

从 jsonpath 和 xpath 到 SPL

关于h5页面苹果手机使用fixed定位tabbar最底部时遮挡内容问题

第 1 篇:搭建OpenGL环境

1-4metaploitable2 introduction

Swift Extension NetworkUtil(网络监听)(源码)

5-if语句(选择结构)
随机推荐
3-list introduction
一文理解同步FIFO
decltype用法介绍
调用Feign接口时指定ip
Vulnhub靶机:BOREDHACKERBLOG_ CLOUD AV
Review of postgraduate English final exam
语料库数据处理个案实例(读取多个文本文件、读取一个文件夹下面指定的多个文件、解码错误、读取多个子文件夹文本、多个文件批量改名)
auto使用示例
Cloud development who is the source code of undercover applet
Part 2: drawing a window
POM configuration provided and test
第 1 篇:搭建OpenGL环境
. No main manifest attribute in jar
Chrono usage notes
[008] filter the table data row by row, jump out of the for cycle and skip this cycle VBA
Vulnhub target: boredhackerblog: social network
3-列表简介
Ke Yi fallible point
OC Extension 检测手机是否安装某个App(源码)
第 2 篇:繪制一個窗口