導入頭文件:#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