zhangpengfei00 发表于 2015-9-30 11:49:58

利用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 = ;
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 = [[ alloc] init];
//any other specail init as required
      }
}
return g_instance;
}

/**
* @brief         get the network statue
*/
- (BOOL)isNetworkReachable
{
BOOL isReachable = NO;
Reachability *reachability = ;
switch () {
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 ([ currentReachabilityStatus] != NotReachable);
}
/**
* @brief         To judge whether the 3G connection
*/
- (BOOL)isEnable3G
{
return ([ currentReachabilityStatus] != NotReachable);
}

@end
  
  调用方法:



#import "NetworkEnvironment.h"
if (NO == [ isNetworkReachable]) {
;
  
  2、网络连接过程中实时监控网络状况(网络变化)
  首先引入头文件:
#import "Reachability.h"
  .h文件中定义



Reachability *hostReach;
  .m文件如下:



//wifi下自动更新,设置接受通知
if ([[ objectForKey:@"UPDATESETTING"] isEqualToString:@"WIFI_AUTO"]) {
// 设置网络状态变化时的通知函数
      [ addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
hostReach = [ retain];
}

#pragma mark - Publicmethods
-(void)reachabilityChanged:(NSNotification *)note
{
Reachability * curReach = ;
NSParameterAssert(]);
;
}
-(void)updateInterfaceWithReachability:(Reachability *)curReach
{
NetworkStatus status = ;
//由其他环境变为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.iyunv.com/211214/695979
页: [1]
查看完整版本: 利用Reachability判断网络环境(WAN/WIFI)