• iOS开发 利用Reachability判断网络环境


    导入头文件:#import "Reachability.h"

     然后将 SystemConfiguration.framework 添加进工程:

     

    1、检查当前的网络状态(wifi、WAN还是无网络) 

    NetworkEnvironment.h:

    复制代码
    #import <Foundation/Foundation.h>
    #import "Reachability.h"
    
    @interface NetworkEnvironment : NSObject
    
    /**
     * @brief           get the signalton engine object
     * @return          the engine object 
     */
    + (NetworkEnvironment *)sharedInstance;
    
    /**
     * @brief           get the network statue 
     */
    - (BOOL)isNetworkReachable;
    
    /**
     * @brief           Judgment wifi is connected
     */
    - (BOOL)isEnableWIFI;
    
    /**
     * @brief           To judge whether the 3G connection
     */
    - (BOOL)isEnable3G;
    
    @end
    复制代码

     

    NetworkEnvironment.m:

    复制代码
    #import "NetworkEnvironment.h"
    #import "Reachability.h"
    
    @interface NetworkEnvironment ()
    
    @end
    
    @implementation NetworkEnvironment
    
    static NetworkEnvironment *g_instance = nil;
    
    
    - (id)init
    {
        self = [super init];
        if (self) {
            
        }
        return self;
    }
    
    
    /**
     * @brief           Whether there are single instance
     * @return          the result 
     */
    + (BOOL)sharedInstanceExists
    {
        return (nil != g_instance);
    }
    
    /**
     * @brief           get the signalton engine object
     * @return          the engine object 
     */
    + (NetworkEnvironment *)sharedInstance
    {
        @synchronized(self) {
            if ( g_instance == nil ) {
                g_instance = [[[self  class] alloc] init];
                //any other specail init as required 
            }
        }
        return g_instance;
    }
    
    
    /**
     * @brief           get the network statue 
     */
    - (BOOL)isNetworkReachable
    {
        BOOL isReachable = NO;
        Reachability *reachability = [Reachability reachabilityWithHostname:@"www.baidu.com"];
        switch ([reachability currentReachabilityStatus]) {
            case NotReachable:{
                isReachable = NO;
            }
                break;
            case ReachableViaWWAN:{
                isReachable = YES;
            }
                break;
            case ReachableViaWiFi:{
                isReachable = YES;   
            }
                break;
            default:
                isReachable = NO;
                break;
        }
        return isReachable;
    }
    
    /**
     * @brief           Judgment wifi is connected
     */
    - (BOOL)isEnableWIFI
    {
        return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
    }
    
    /**
     * @brief           To judge whether the 3G connection
     */
    - (BOOL)isEnable3G
    {
        return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
    }
    
    
    @end
    复制代码

    调用方法:

    #import "NetworkEnvironment.h"
    
    if (NO == [[NetworkEnvironment sharedInstance] isNetworkReachable]) {
            [WBCommonHelper showHUDWithText:@"网络状况异常"];

    2、网络连接过程中实时监控网络状况(网络变化)

    首先引入头文件:

    #import "Reachability.h"

    .h文件中定义

     Reachability *hostReach;

    .m文件如下:

    复制代码
    //wifi下自动更新,设置接受通知
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"UPDATESETTING"] isEqualToString:@"WIFI_AUTO"]) {
            // 设置网络状态变化时的通知函数
            [[NSNotificationCenter defaultCenter] addObserver:self 
                                                     selector:@selector(reachabilityChanged:) 
                                                         name:kReachabilityChangedNotification  
                                                       object:nil];
            hostReach = [[Reachability reachabilityWithHostname:@"www.baidu.com"] retain];
        }
    
    
    #pragma mark - Public  methods
    -(void)reachabilityChanged:(NSNotification *)note
    {
        Reachability * curReach = [note object];
        NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
        [selfupdateInterfaceWithReachability:curReach];
    }
    
    -(void)updateInterfaceWithReachability:(Reachability *)curReach
    {
        NetworkStatus status = [curReach currentReachabilityStatus];
        //由其他环境变为wifi环境
        if (status == ReachableViaWiFi)
        {
             NSLog(@"切换到WIFi环境");
        }
    }
    复制代码

     

    Reachability.h中定义了三种网络状态:
        typedef enum {
            NotReachable = 0,            //无连接
            ReachableViaWiFi,            //使用3G/GPRS网络
            ReachableViaWWAN            //使用WiFi网络
        } NetworkStatus;

  • 相关阅读:
    如何 Laravel 中验证 zip 压缩包里的文件?
    PHP7的Yaconf使用教程
    算法与数据结构系列 ( 三 )
    推荐10个优质的Laravel扩展
    如何在利用 Composer 注册全局辅助函数?
    ThinkPHP6新增‘’多应用‘’与ThinkPHP5有啥区别
    基于Laravel开发的在线点播系统MeEdu
    浅述PHP7底层设计01-PHP7语言执行原理
    laravel单文件、多文件上传的实现方法
    在Mac开发环境Laravel Valet中配置运行Flarum论坛系统的方法详解
  • 原文地址:https://www.cnblogs.com/diweinan/p/6214151.html
Copyright © 2020-2023  润新知