123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- //
- // ReachabilitySwiftManager.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2019/5/28.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- //
- import UIKit
- import Reachability
- class ReachabilitySwiftManager: NSObject {
- static let shared : ReachabilitySwiftManager = ReachabilitySwiftManager()
-
- public lazy var reachability: Reachability = {
- let reachability = Reachability()!
- return reachability
- }()
-
- /// 初始化网络监听
- public func initStartReachability() {
- whenReachable()
- whenUnreachable()
- startNotifier()
- }
-
-
- /// 初始化网络监听需要他停止监听
- public func initStartStopReachability() {
- whenReachableStopNotifier()
- whenUnreachableStopNotifier()
- startNotifier()
- }
-
- /// 监听到连接
- private func whenReachable() {
- reachability.whenReachable = { reachability in
- if reachability.connection == .wifi {
- NXLLog("Reachable via WiFi")
- } else if reachability.connection == .cellular {
- NXLLog("Reachable via Cellular")
- }
- }
- }
-
- /// 监听到没有连接
- private func whenUnreachable() {
- reachability.whenUnreachable = { [weak self] _ in
- self?.alert()
- NXLLog("Not reachable")
- }
- }
-
- /// 监听到连接需要停止监听
- private func whenReachableStopNotifier() {
- reachability.whenReachable = { [weak self] reachability in
- if reachability.connection == .wifi {
- NXLLog("Reachable via WiFi")
-
- } else if reachability.connection == .cellular {
- NXLLog("Reachable via Cellular")
- }
- self?.stopNotifier()
- }
- }
- /// 监听到没有连接需要停止监听
- private func whenUnreachableStopNotifier() {
- reachability.whenUnreachable = { _ in
- AlertSheetView.alert(title: "网络连接失败", detailTitle: "检测到网络权限可能未开启,您可以在\"设置\"中检查蜂窝移动网络", cancelTitle: "取消", sureTitle: "设置", cancelBlock: {[weak self] (popupView, index, str) in
- self?.stopNotifier()
- }, confirmBlock: { (popupView, index, str) in
- DispatchQueue.main.async(execute: { [weak self] () -> Void in
- let url = URL(string: UIApplication.openSettingsURLString)
- if let url = url, UIApplication.shared.canOpenURL(url) { if #available(iOS 10.0, *) {
- UIApplication.shared.open(url, options: [:],completionHandler: {
- [weak self] (success) in
- self?.stopNotifier()
- })
- }else {
- UIApplication.shared.openURL(url)
- self?.stopNotifier()
- }
-
- }
-
- })
- })
- NXLLog("Not reachable")
- }
- }
-
- /// 开始监听
- private func startNotifier() {
- do {
- try reachability.startNotifier()
- } catch {
- startNotifier()
- NXLLog("Unable to start notifier")
- }
- }
-
-
- /// 停止监听
- private func stopNotifier() {
- reachability.stopNotifier()
- }
-
- /// 弹框跳转设置页面
- public func alert(){
- AlertSheetView.alert(title: "网络连接失败", detailTitle: "检测到网络权限可能未开启\n您可以在\"设置>无线数据>WLAN或WLAN与蜂窝移动网\"开启一下吧", cancelTitle: "取消", sureTitle: "设置", cancelBlock:nil, confirmBlock: { (popupView, index, str) in
- DispatchQueue.main.async(execute: {() -> Void in
- let url = URL(string: UIApplication.openSettingsURLString)
- if let url = url, UIApplication.shared.canOpenURL(url) { if #available(iOS 10.0, *) {
- UIApplication.shared.open(url, options: [:],completionHandler: {(success) in})
- }else {
- UIApplication.shared.openURL(url)
- }
- }
- })
- })
- }
- }
|