Commit ed5026fa564a6940eb065a01f215d6b58a7298f9
1 parent
9ac40b09
首页监听网络变化
Showing
2 changed files
with
34 additions
and
6 deletions
metaCode/Classes/Main/HomePage/Controller/CNMetaCodeHomePageController.swift
| ... | ... | @@ -184,6 +184,11 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel |
| 184 | 184 | |
| 185 | 185 | self.setupUI() |
| 186 | 186 | |
| 187 | + /// 通知名 | |
| 188 | + let notificationName = "CNLiveNetworkStatusNotification" | |
| 189 | + /// 自定义通知 | |
| 190 | + NotificationCenter.default.addObserver(self, selector: #selector(networkStatusNotificationAction), name: NSNotification.Name(rawValue: notificationName), object: nil) | |
| 191 | + | |
| 187 | 192 | // self.tabBarController?.tabBar.top = self.tabBarY + KSreenHeight |
| 188 | 193 | |
| 189 | 194 | } |
| ... | ... | @@ -199,7 +204,11 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel |
| 199 | 204 | |
| 200 | 205 | self.topNavView.isHidden = true |
| 201 | 206 | } |
| 202 | - | |
| 207 | + | |
| 208 | + deinit { | |
| 209 | + /// 移除通知 | |
| 210 | + NotificationCenter.default.removeObserver(self) | |
| 211 | + } | |
| 203 | 212 | |
| 204 | 213 | // MARK: - 添加UI |
| 205 | 214 | func setupUI(){ |
| ... | ... | @@ -223,12 +232,26 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel |
| 223 | 232 | |
| 224 | 233 | } |
| 225 | 234 | |
| 226 | - // MARK: -按钮点击事件 | |
| 235 | + // MARK: -事件 | |
| 227 | 236 | //无网时刷新 |
| 228 | 237 | @objc func errorFreshButtonAction(){ |
| 229 | 238 | self.getUserIdentityApi() |
| 230 | 239 | } |
| 231 | - | |
| 240 | + /// 接受到通知后的方法回调 | |
| 241 | + @objc private func networkStatusNotificationAction(noti: Notification) { | |
| 242 | + let netStatus:String = noti.object as! String | |
| 243 | + switch netStatus { | |
| 244 | +// case "notReachable"://暂时不处理 | |
| 245 | + | |
| 246 | + case "reachableViaWWAN": | |
| 247 | + self.getUserIdentityApi() | |
| 248 | + case "reachableViaWiFi": | |
| 249 | + self.getUserIdentityApi() | |
| 250 | + default: | |
| 251 | + break | |
| 252 | + } | |
| 253 | + } | |
| 254 | + | |
| 232 | 255 | // MARK: -添加约束 |
| 233 | 256 | func makeConstraints(){ |
| 234 | 257 | self.errorImgView.snp.makeConstraints { make in | ... | ... |
metaCode/Classes/Other/AppDelegate.swift
| ... | ... | @@ -20,14 +20,19 @@ class AppDelegate: UIResponder, UIApplicationDelegate { |
| 20 | 20 | CNLiveNetworking.networkStatus { status in |
| 21 | 21 | |
| 22 | 22 | if(status == .notReachable || status == .unknown){ |
| 23 | - print("notReachable") | |
| 23 | + /// 发送简单数据 | |
| 24 | + NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "CNLiveNetworkStatusNotification"), object: "notReachable") | |
| 24 | 25 | } |
| 25 | 26 | if status == .reachableViaWWAN { |
| 26 | - print("reachableViaWWAN") | |
| 27 | + /// 发送简单数据 | |
| 28 | + NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "CNLiveNetworkStatusNotification"), object: "reachableViaWWAN") | |
| 27 | 29 | } |
| 28 | 30 | if status == .reachableViaWiFi { |
| 29 | - print("reachableViaWiFi") | |
| 31 | + /// 发送简单数据 | |
| 32 | + NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "CNLiveNetworkStatusNotification"), object: "reachableViaWiFi") | |
| 30 | 33 | } |
| 34 | + | |
| 35 | + | |
| 31 | 36 | } |
| 32 | 37 | // if statue == .notReachable || statue == .unknown { |
| 33 | 38 | //// NSNotification(name: "network_statue", object: "") | ... | ... |