您的位置:首页 > 理论基础 > 计算机网络

swift之判断网络状态Alamofire、Reachability

2018-01-26 10:51 363 查看
==============//Alamofire监控网络

 func monitorNet()  {

    let manager = NetworkReachabilityManager(host: "www.apple.com")

    manager?.listener = { status in

    print("网络状态: \(status)")

    }

    manager?.startListening()//开始监听网络

    }

================Reachability.swift监控网络

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

 

    var reachability:
Reachability?

    var netWorkIsAvailable

    

    func monitorNet(){

        do {

            reachability = try Reachability.reachabilityForInternetConnection()

        } catch {

            print("unable to create Reachability")

            return

        }

        if reachability.isReachable() {

            print("appDelegate:网络连接:可用")

            netWorkIsAvailable = true

        } else {

            print("appDelegate:网络连接:不可用")

            netWorkIsAvailable = false

        }

        

        if reachability!.isReachableViaWiFi() {

            print("连接类型:WiFi")

        } else if reachability!.isReachableViaWWAN() {

            print("连接类型:移动网络")

        } else {

            print("连接类型:没有网络连接")

        }

        

        

        do {

            try reachability?.startNotifier()//开始监测

        } catch {

            print("could not start reachability notifier")

        }

    }

        

    

    

// 网络可用或切换网络类型时执行

reachability.whenReachable = { reachability in

    

    // 判断网络状态及类型

}

// 网络不可用时执行

reachability.whenUnreachable = { reachability in

    

    // 判断网络状态及类型

}

    

// ======= ==== = ================

    /**

     通过消息自动监听网络改变

     添加消息监听

     */

    

    func NetworkStatusListener() {

        // 1、设置网络状态消息监听

        NSNotificationCenter.defaultCenter().addObserver(self, selector:"networkStatusChange", name: ReachabilityChangedNotification, object:nil);

        

        // 2、获得网络Reachability对象

        // Reachability必须一直存在,所以需要设置为全局变量

        conn = Reachability.reachabilityForInternetConnection()!;

        // 3、开启网络状态消息监听

        conn!.startNotifier();

    }

    

    func networkStatusChange() {

        checkNetworkStatus();

    }

    

    /**

     移除消息通知

     */

    deinit {

        // 关闭网络状态消息监听

        conn!.stopNotifier();

        // 移除网络状态消息通知

        NSNotificationCenter.defaultCenter().removeObserver(self);

    }

    

    /**

     主动检测网络状态

     */

    func checkNetworkStatus() {

        let reachability = Reachability.reachabilityForInternetConnection()// 准备获取网络连接信息

        

        if reachability!.isReachable() {
// 判断网络连接状态

            print("网络连接:可用")

            if reachability!.isReachableViaWiFi() {// 判断网络连接类型

                print("连接类型:WiFi")

            } else
if reachability!.isReachableViaWWAN() {

                print("连接类型:移动网络")

            }

        }else{

            print("网络连接:不可用")

            print("连接类型:没有网络连接")

        }

    }

    

    

    //    //移除通知

    //    func onButtonClicked(sender: AnyObject) {

    //        reachability!.stopNotifier()

    //        NSNotificationCenter.defaultCenter().removeObserver(

    //            self,

    //            name: ReachabilityChangedNotification,

    //            object: reachability

    //        )

    //    }

==================swift使用OC的reachalibity利用桥接来完成;

创建OC的LYBReachability.h和.m文件后会生成桥接文件FastappSwift-Bridging-Header.h;在这个桥接文件中引入

#import "LYBReachability.h";

*************

#import <Foundation/Foundation.h>

#import <Reachability/Reachability.h>

@interface LYBReachability :
NSObject

+ (BOOL)setNet;

@end
*************

#import "LYBReachability.h"

@implementation LYBReachability

+ (BOOL)setNet{

    Reachability * re=[Reachability
reachabilityWithHostName:@"www.baidu.com"];

    if ([re
currentReachabilityStatus] == ReachableViaWiFi || [re
currentReachabilityStatus] == ReachableViaWWAN) {

        return YES;

    }else{

        return NO;

    }

}

@end
****************
使用:

 func Alamofire()  {

        if
LYBReachability.setNet(){

            print("有网络")

        } else{

        return NO;

               }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: