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

时间:2024-01-21 09:48:15

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

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

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

NetworkEnvironment.h:

iOS开发 利用Reachability判断网络环境
#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
iOS开发 利用Reachability判断网络环境

NetworkEnvironment.m:

iOS开发 利用Reachability判断网络环境
#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
iOS开发 利用Reachability判断网络环境

调用方法:

#import "NetworkEnvironment.h"

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

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

首先引入头文件:

#import "Reachability.h"

.h文件中定义

 Reachability *hostReach;

.m文件如下:

iOS开发 利用Reachability判断网络环境
//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环境");
}
}
iOS开发 利用Reachability判断网络环境

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