當(dāng)應(yīng)用程序需要訪問網(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)聽設(shè)備的網(wǎng)絡(luò)狀態(tài)的改變,當(dāng)設(shè)備網(wǎng)絡(luò)狀態(tài)連接、斷開時(shí),程序也應(yīng)該有相應(yīng)的處理。
成都創(chuàng)新互聯(lián)公司專注于臨縣網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供臨縣營(yíng)銷型網(wǎng)站建設(shè),臨縣網(wǎng)站制作、臨縣網(wǎng)頁設(shè)計(jì)、臨縣網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造臨縣網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供臨縣網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
工欲善其事必先利器,在檢查設(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。
打開Main.storyboard界面設(shè)計(jì)文件,向該文件中添加1個(gè)UILabel,1個(gè)UITextFieldhe 3個(gè)UIButton,如下圖所示(^_^不好意思,最下面2個(gè)UILabel是打廣告的)。為了在程序中訪問界面上的文本框,將文本框綁定到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:事件處理方法。
接下來編輯該示例的視圖控制器類,該視圖控制器類的實(shí)現(xiàn)部分主要依靠Reachability類來檢測(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:@"不能訪問%@", site]]; break; case ReachableViaWWAN: [self showAlert:[NSString stringWithFormat:@"使用3G/4G網(wǎng)絡(luò)訪問%@", site]]; break; case ReachableViaWiFi: [self showAlert:[NSString stringWithFormat:@"使用Wifi網(wǎng)絡(luò)訪問%@", 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:類方法來獲取Reachability對(duì)象,然后調(diào)用該對(duì)象的currentReachabilityStatus方法來獲取訪問指定站點(diǎn)的方式,該方法返回NetworkStatus枚舉值,該枚舉值有如下3個(gè):
typedef enum{ NotReachable= 0, //無連接ReachableViaWiFi, //使用3G/4G網(wǎng)絡(luò)ReachableViaWWAN //使用WiFi網(wǎng)絡(luò) }NetworkStatus; |
上面程序?qū)eachability的currentReachabilityStatus方法返回值進(jìn)行判斷,這樣即可獲取該應(yīng)用訪問網(wǎng)絡(luò)的狀態(tài)和方式。
編譯、運(yùn)行該程序,如對(duì)www.cnblogs.com進(jìn)行“測(cè)試”,效果如下圖。
如果訪問的站點(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)聽網(wǎng)絡(luò)狀態(tài)的改變。當(dāng)網(wǎng)絡(luò)斷開連接時(shí),提醒用戶,網(wǎng)絡(luò)連接已經(jīng)斷開,應(yīng)用可能需要暫停;當(dāng)網(wǎng)絡(luò)重新連接時(shí),再次提醒用戶,應(yīng)用可以繼續(xù)運(yùn)行。程序獲取Reachability對(duì)象之后,調(diào)用Reachability對(duì)象的startNotifier方法即可開啟該對(duì)象的被監(jiān)聽狀態(tài)——當(dāng)Reachability的連接狀態(tài)發(fā)生改變時(shí),該對(duì)象將會(huì)發(fā)送一個(gè)kReachabilityChangedNotification通知給默認(rèn)的通知中心,因此程序只要使用默認(rèn)的通知中心監(jiān)聽該通知即可。
為了監(jiān)聽網(wǎng)絡(luò)狀態(tài)的改變,在應(yīng)用程序委托類(AppDelegate.m)的application: didFinishLaunchingWithOptions:方法中增加如下代碼:
//使用通知中心監(jiān)聽kReachabilityChangedNotification通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; //獲取訪問指定站點(diǎn)的Reachability對(duì)象 Reachability *reach = [Reachability reachabilityWithHostName:@"www.cnblogs.com"]; //讓Reachability對(duì)象開啟被監(jiān)聽狀態(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 { //通過通知對(duì)象獲取被監(jiān)聽的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:@"不能訪問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)行提醒?!?/p>
本文標(biāo)題:檢查網(wǎng)絡(luò)狀態(tài)
文章轉(zhuǎn)載:http://aaarwkj.com/article26/pdhdjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、搜索引擎優(yōu)化、建站公司、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)