检查网络状态
? ? ? 當(dāng)應(yīng)用程序需要訪問(wèn)網(wǎng)絡(luò)時(shí),它首先應(yīng)該檢查設(shè)備的網(wǎng)絡(luò)狀態(tài),確認(rèn)設(shè)備的網(wǎng)絡(luò)環(huán)境及連接情況,并針對(duì)這些情況提醒用戶做出相應(yīng)的處理。最好能監(jiān)聽(tīng)設(shè)備的網(wǎng)絡(luò)狀態(tài)的改變,當(dāng)設(shè)備網(wǎng)絡(luò)狀態(tài)連接、斷開(kāi)時(shí),程序也應(yīng)該有相應(yīng)的處理。
工欲善其事必先利器,在檢查設(shè)備的網(wǎng)絡(luò)狀態(tài)前,我們要先實(shí)現(xiàn)兩個(gè)步驟:
下載,添加Reachability類。
下載Reachability.zip壓縮包,最新的版本為V3.5,解壓該壓縮包會(huì)得到一個(gè)Xcode項(xiàng)目,其實(shí)關(guān)鍵是得到改項(xiàng)目的Reachability.h和 Reachability.m文件,并把它們添加到項(xiàng)目中。
2. 為項(xiàng)目添加SystemConfiguration.framework框架。
? ? 添加方法:
1) ? 選中項(xiàng)目名稱
2)選中TARGETS
3)選中Build Phases
4)在Link Binary With Libraries中添加。
將Reachability.h和 Reachability.m文件添加到項(xiàng)目中。
注意:如果Reachability不是3.0以上的版本,而是Reachability 2.x版本,它是不支持ARC的。本項(xiàng)目已經(jīng)啟用了ARC,早期版本的Reachability類并不支持ARC,因此需要手動(dòng)設(shè)置該類禁用ARC。
打開(kāi)Main.storyboard界面設(shè)計(jì)文件,向該文件中添加1個(gè)UILabel,1個(gè)UITextFieldhe 3個(gè)UIButton,如下圖所示(^_^不好意思,最下面2個(gè)UILabel是打廣告的)。為了在程序中訪問(wèn)界面上的文本框,將文本框綁定到siteField IBOutlet屬性。為了讓程序能相應(yīng)界面上3個(gè)按鈕的點(diǎn)擊事件,將“測(cè)試”按鈕的“Touch UP Inside”事件綁定testNetStatus:事件處理方法,為“測(cè)試WIFI”按鈕的“Touch ? UP Inside”事件綁定testWifi:事件處理方法,為“測(cè)試3G/4G”按鈕的“Touch UP Inside”事件綁定testInternet:事件處理方法。
接下來(lái)編輯該示例的視圖控制器類,該視圖控制器類的實(shí)現(xiàn)部分主要依靠Reachability類來(lái)檢測(cè)網(wǎng)絡(luò)狀態(tài)。
核心實(shí)現(xiàn)代碼:
// ViewController.m // NetWorkDemo // // Copyright (c) 2014年 MiracleHe. All rights reserved. // #import "ViewController.h" #import "Reachability.h" @interface ViewController () @end @implementation ViewController @synthesize siteField; - (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. } - (IBAction)testNetStatus:(id)sender {NSString *site = self.siteField.text;Reachability *reach = [Reachability reachabilityWithHostName: site];switch ([reach currentReachabilityStatus]) {case NotReachable:[self showAlert:[NSString stringWithFormat:@"不能訪問(wèn)%@", site]];break;case ReachableViaWWAN:[self showAlert:[NSString stringWithFormat:@"使用3G/4G網(wǎng)絡(luò)訪問(wèn)%@", site]];break;case ReachableViaWiFi:[self showAlert:[NSString stringWithFormat:@"使用Wifi網(wǎng)絡(luò)訪問(wèn)%@", site]];break;}} - (IBAction)testWifi:(id)sender {if ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable) {[self showAlert:@"wifi網(wǎng)絡(luò)已經(jīng)連接"];}else{[self showAlert:@"wifi網(wǎng)絡(luò)不可用。"];} } - (IBAction)testInternet:(id)sender {if ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable) {[self showAlert:@"3G/4G網(wǎng)絡(luò)已經(jīng)連接"];}else{[self showAlert:@"3G/4G網(wǎng)絡(luò)不可用"];} } -(void) showAlert:(NSString*) msg {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"網(wǎng)絡(luò)狀態(tài)" message:msg delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];[alert show];} -(BOOL)textFieldShouldReturn:(UITextField *)textField {[siteField resignFirstResponder];return YES;} - (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. } @end
? ? ? 上面程序首先調(diào)用了Reachability類的reachabilityWithHostName:類方法來(lái)獲取Reachability對(duì)象,然后調(diào)用該對(duì)象的currentReachabilityStatus方法來(lái)獲取訪問(wèn)指定站點(diǎn)的方式,該方法返回NetworkStatus枚舉值,該枚舉值有如下3個(gè):
| typedef enum{ ? ?NotReachable = 0, //無(wú)連接ReachableViaWiFi, //使用3G/4G網(wǎng)絡(luò)ReachableViaWWAN //使用WiFi網(wǎng)絡(luò) }NetworkStatus; |
上面程序?qū)eachability的currentReachabilityStatus方法返回值進(jìn)行判斷,這樣即可獲取該應(yīng)用訪問(wèn)網(wǎng)絡(luò)的狀態(tài)和方式。
編譯、運(yùn)行該程序,如對(duì)www.cnblogs.com進(jìn)行“測(cè)試”,效果如下圖。
如果訪問(wèn)的站點(diǎn)本身不存在,即時(shí)設(shè)備的網(wǎng)絡(luò)處于連接狀態(tài),Reachability對(duì)象的currentReachabilityStatus方法也將返回NotReachable。
如果程序僅需要測(cè)試設(shè)備的WiFi或3G/4G網(wǎng)絡(luò)是否連接,則可先調(diào)用Reachability類的reachabilityForLocalWiFi或reachabilityForInternetConnection類方法獲取Reachability對(duì)象,然后調(diào)用該Reachability對(duì)象的currentReachabilityStatus方法獲取網(wǎng)絡(luò)連接狀態(tài),如果網(wǎng)絡(luò)連接狀態(tài)返回NotReachable,則表明這種類型的網(wǎng)絡(luò)暫未連接。
除了直接檢測(cè)網(wǎng)絡(luò)連接狀態(tài)之外,有時(shí)候程序還需要監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)的改變。當(dāng)網(wǎng)絡(luò)斷開(kāi)連接時(shí),提醒用戶,網(wǎng)絡(luò)連接已經(jīng)斷開(kāi),應(yīng)用可能需要暫停;當(dāng)網(wǎng)絡(luò)重新連接時(shí),再次提醒用戶,應(yīng)用可以繼續(xù)運(yùn)行。程序獲取Reachability對(duì)象之后,調(diào)用Reachability對(duì)象的startNotifier方法即可開(kāi)啟該對(duì)象的被監(jiān)聽(tīng)狀態(tài)——當(dāng)Reachability的連接狀態(tài)發(fā)生改變時(shí),該對(duì)象將會(huì)發(fā)送一個(gè)kReachabilityChangedNotification通知給默認(rèn)的通知中心,因此程序只要使用默認(rèn)的通知中心監(jiān)聽(tīng)該通知即可。
為了監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)的改變,在應(yīng)用程序委托類(AppDelegate.m)的application: didFinishLaunchingWithOptions:方法中增加如下代碼:
| //使用通知中心監(jiān)聽(tīng)kReachabilityChangedNotification通知 ? ?[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; ? ?//獲取訪問(wèn)指定站點(diǎn)的Reachability對(duì)象 ? ?Reachability *reach = [Reachability reachabilityWithHostName:@"www.cnblogs.com"]; ? ?//讓Reachability對(duì)象開(kāi)啟被監(jiān)聽(tīng)狀態(tài) ? ?[reach startNotifier]; |
? ? ? 上面的代碼使用默認(rèn)的通知中心檢測(cè)kReachabilityChangedNotification通知,這意味著當(dāng)Reachability的連接狀態(tài)發(fā)生改變時(shí),默認(rèn)的通知中心就會(huì)收到該通知,從而觸發(fā)應(yīng)用程序委托類的reachabilityChanged:方法,還需要在應(yīng)用程序委托類中定義如下方法:
| - (void) reachabilityChanged:(NSNotification*) note { ? ?//通過(guò)通知對(duì)象獲取被監(jiān)聽(tīng)的Reachability對(duì)象 ? ?Reachability *curReach = [note object]; ? ?//獲取Reachability對(duì)象的網(wǎng)絡(luò)狀態(tài) ? ?NetworkStatus status = [curReach currentReachabilityStatus]; ? ?if (status == NotReachable) { ? ? ? ?UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"不能訪問(wèn)www.cnblogs.com" delegate:nil cancelButtonTitle:@"YES" otherButtonTitles: nil]; ? ? ? ?[alert show]; ? ?} } |
? ? ? reachabilityChanged:會(huì)判斷該Reachability對(duì)象的網(wǎng)絡(luò)連接狀態(tài),當(dāng)該對(duì)象的網(wǎng)絡(luò)連接狀態(tài)處于NotReachable時(shí),程序會(huì)使用UIAlertView進(jìn)行提醒?!?/span>
轉(zhuǎn)載于:https://blog.51cto.com/8887511/1405996
總結(jié)
- 上一篇: 我的欧拉工程之路_3
- 下一篇: 持续测试的益处