当前位置:网站首页>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
}
}
}
}
边栏推荐
- Opencvsharp binary image anti color
- Resolution error: LNK2019 unresolved external symbol
- Open cooperation and win-win future | Fuxin Kunpeng joins Jinlan organization
- Vulnhub target: boredhackerblog: social network
- 解决 These dependencies were not found: * core-js/modules/es6.array.fill in xxx 之类的问题
- 第 2 篇:绘制一个窗口
- Smart pointer remarks
- 不止于观测|阿里云可观测套件正式发布
- [test development] first knowledge of software testing
- Vulnhub靶机:BOREDHACKERBLOG_ CLOUD AV
猜你喜欢

Vulnhub target: boredhackerblog_ CLOUD AV

1-4metaploitable2 introduction

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

2022 PMP project management examination agile knowledge points (1)

Hongmeng OS development III

GraphMAE----論文快速閱讀

研究生英语期末考试复习

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

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

Chapitre 2: dessiner une fenêtre
随机推荐
Thread support
Error "computing failed in `stat\u summary\u hex() `"
Atguigu---15- built in instruction
3-list introduction
JS implementation to check whether an array object contains values from another array object
BOM notes
Moonwell Artemis现已上线Moonbeam Network
LeetCode练习——跳跃游戏、组合求和
模型效果优化,试一下多种交叉验证的方法(系统实操)
Moonwell Artemis is now online moonbeam network
『C语言』系统日期&时间
ImportError: cannot import name ‘process_ pdf‘ from ‘pdfminer. Pdfinterp 'error completely resolved
本地备份和还原 SQL Server 数据库
Echart 心得 (一): 有关Y轴yAxis属性
第 1 篇:搭建OpenGL环境
Oracle-高级SQL限定查询
Thread blocking
Chapter 1 overview of canvas
毕业两年月薪36k,说难也不难吧
Tuple remarks