利用Reachability判断网络环境(WAN/WIFI)

导入头文件:#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;

 

 网上文章:

1.ios利用Reachability确认网络环境3G/WIFI :http://hi.baidu.com/feng20068123/item/275eb5c2d9bf0a68f6c95d63

2.当使用Reachability出错的时候:http://rainbird.blog.51cto.com/211214/695979

转载于:https://www.cnblogs.com/ios-wmm/archive/2013/05/05/2697421.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值