swift之判断网络状态Alamofire、Reachability

网络状态Alamofire、Reachability.h都可以实现

根据状态栏实现网络监测https://www.jianshu.com/p/247262b80f5f

ios13开始要在capability中开启Access Wifi  info

==============//Alamofire监控网络

    //Alamofire监控网络,只能调用一次监听一次
class func AlamofiremonitorNet() {
var manager: NetworkReachabilityManager?
manager = NetworkReachabilityManager(host: "www.apple.com")
manager?.startListening { status in
    print("网络状态: \(status)")
    if status == .reachable(.ethernetOrWiFi) { //WIFI
        print("wifi")
    } else if status == .reachable(.cellular) { // 蜂窝网络
        print("4G")
    } else if status == .notReachable { // 无网络
        print("无网络")
    } else { // 其他
        
    }
}
}

 

 

Reachability    监控网络  https://github.com/tonymillion/Reachability---需要桥接

https://github.com/tonymillion/Reachability和Reachability.swift(ios12不可用)不是同一个,

这里需要在桥接文件中加入#import "Reachability.h"

//实时监听网络状态
/*
     这个每次调用都可以监测,
     从有网到无网,状态改变可以监测到,但是从无网到有网不能监测。需要重新调用。
     使用中可以做一个标记,如果标记是有网true,就不调用下面的方法,如果标记是无网false就调用下面的方法,如果判断是有网的,改变标记为true,否则标记为false
     */

        // let reach  = Reachability.forInternetConnection()
    let reach  =  Reachability.init(hostName: "www.baidu.com")
        reach?.reachableBlock = { reachabilty in
            print("有网络\(String(describing: reachabilty))")
            if (reachabilty?.isReachableViaWiFi())!{
                            print("wifi")
                }else if (reachabilty?.isReachableViaWWAN())!{
                            print("4G")
                }else {
                            print("无网络")
                    }
        }

        reach?.unreachableBlock = {
     reachabilty in
             print("无网络\(String(describing: reachabilty))")
            
        }
        
        reach?.startNotifier()

直接检测一次网络*****

   let reach  = Reachability.forInternetConnection()
        
        if (reach?.isReachableViaWiFi())!{
            print("wifi")
        }else if (reach?.isReachableViaWWAN())!{
            print("4G")
        }else {
            print("无网络")
        }

通知的方式监测网络状态*********不起作用

 //通知监听网络---这个没起作用
    func notificationMonitorNetwork(){
       
    // let reach  =  Reachability.init(hostName: "www.baidu.com")
        let reach = Reachability.forInternetConnection()
        NotificationCenter.default.addObserver(self, selector: #selector(rechChange), name: NSNotification.Name.init(rawValue: "reachabilityChanged"), object: nil)
        reach?.startNotifier()
    }
  @objc  func rechChange(note: NSNotification) {
    print("检测网络")
       let reach = note.object as! Reachability // 准备获取网络连接信息
    if reach.isReachableViaWiFi() || reach.isReachableViaWWAN() {
            print("有网络")
        } else {
            print("没有网络")
        }
  
    }

 

================Reachability.swift监控网络,目前ios12不能用

下载地址https://github.com/ashleymills/Reachability.swift

使用版本:iOS (8.0 - 12.0), OSX (10.9 - 10.14) and tvOS (9.0 - 12.0)

    //Reachability.swift监听网络,以闭包的方式

   class func reachabilitymonitornetonce(){
        let reachability = try! Reachability()

        reachability.whenReachable = { reachability in
            if reachability.connection == .wifi {
                print("Reachable via WiFi")
            } else {
                print("Reachable via Cellular")
            }
        }
        reachability.whenUnreachable = { _ in
            print("Not reachable")
        }

        do {
            try reachability.startNotifier()
        } catch {
            print("Unable to start notifier")
        }
    
    }


 //Reachability.swift监听网络,以通知的方式
//declare this inside of viewWillAppear
   class func reachabilitymonitornet(){
    let reachability = try! Reachability()

NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
        do{
              try reachability.startNotifier()
            }catch{
              print("could not start reachability notifier")
            }

    }
    @objc func reachabilityChanged(note: Notification) {

      let reachability = note.object as! Reachability

      switch reachability.connection {
      case .wifi:
          print("Reachable via WiFi")
      case .cellular:
          print("Reachable via Cellular")
      case .unavailable:
        print("Network not reachable")
      case .none: break
        
      }
    }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值