日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

检查网络状态

發布時間:2025/3/17 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 检查网络状态 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? 當應用程序需要訪問網絡時,它首先應該檢查設備的網絡狀態,確認設備的網絡環境及連接情況,并針對這些情況提醒用戶做出相應的處理。最好能監聽設備的網絡狀態的改變,當設備網絡狀態連接、斷開時,程序也應該有相應的處理。

  工欲善其事必先利器,在檢查設備的網絡狀態前,我們要先實現兩個步驟:

  • 下載,添加Reachability類。

  • 下載Reachability.zip壓縮包,最新的版本為V3.5,解壓該壓縮包會得到一個Xcode項目,其實關鍵是得到改項目的Reachability.h和 Reachability.m文件,并把它們添加到項目中。

    2. 為項目添加SystemConfiguration.framework框架。

    ? ? 添加方法:

    • 1) ? 選中項目名稱

    • 2)選中TARGETS

    • 3)選中Build Phases

    • 4)在Link Binary With Libraries中添加。


    將Reachability.h和 Reachability.m文件添加到項目中。

      注意:如果Reachability不是3.0以上的版本,而是Reachability 2.x版本,它是不支持ARC的。本項目已經啟用了ARC,早期版本的Reachability類并不支持ARC,因此需要手動設置該類禁用ARC。

      打開Main.storyboard界面設計文件,向該文件中添加1個UILabel,1個UITextFieldhe 3個UIButton,如下圖所示(^_^不好意思,最下面2個UILabel是打廣告的)。為了在程序中訪問界面上的文本框,將文本框綁定到siteField IBOutlet屬性。為了讓程序能相應界面上3個按鈕的點擊事件,將“測試”按鈕的“Touch UP Inside”事件綁定testNetStatus:事件處理方法,為“測試WIFI”按鈕的“Touch ? UP Inside”事件綁定testWifi:事件處理方法,為“測試3G/4G”按鈕的“Touch UP Inside”事件綁定testInternet:事件處理方法。



    接下來編輯該示例的視圖控制器類,該視圖控制器類的實現部分主要依靠Reachability類來檢測網絡狀態。

      核心實現代碼:

    // 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網絡訪問%@", site]];break;case ReachableViaWiFi:[self showAlert:[NSString stringWithFormat:@"使用Wifi網絡訪問%@", site]];break;}} - (IBAction)testWifi:(id)sender {if ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable) {[self showAlert:@"wifi網絡已經連接"];}else{[self showAlert:@"wifi網絡不可用。"];} } - (IBAction)testInternet:(id)sender {if ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable) {[self showAlert:@"3G/4G網絡已經連接"];}else{[self showAlert:@"3G/4G網絡不可用"];} } -(void) showAlert:(NSString*) msg {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"網絡狀態" 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


    ? ? ? 上面程序首先調用了Reachability類的reachabilityWithHostName:類方法來獲取Reachability對象,然后調用該對象的currentReachabilityStatus方法來獲取訪問指定站點的方式,該方法返回NetworkStatus枚舉值,該枚舉值有如下3個:


    typedef enum{ ? ?NotReachable = 0, //無連接ReachableViaWiFi, //使用3G/4G網絡ReachableViaWWAN //使用WiFi網絡 }NetworkStatus;


    上面程序對Reachability的currentReachabilityStatus方法返回值進行判斷,這樣即可獲取該應用訪問網絡的狀態和方式。


      編譯、運行該程序,如對www.cnblogs.com進行“測試”,效果如下圖。

      如果訪問的站點本身不存在,即時設備的網絡處于連接狀態,Reachability對象的currentReachabilityStatus方法也將返回NotReachable。

      如果程序僅需要測試設備的WiFi或3G/4G網絡是否連接,則可先調用Reachability類的reachabilityForLocalWiFi或reachabilityForInternetConnection類方法獲取Reachability對象,然后調用該Reachability對象的currentReachabilityStatus方法獲取網絡連接狀態,如果網絡連接狀態返回NotReachable,則表明這種類型的網絡暫未連接。


      除了直接檢測網絡連接狀態之外,有時候程序還需要監聽網絡狀態的改變。當網絡斷開連接時,提醒用戶,網絡連接已經斷開,應用可能需要暫停;當網絡重新連接時,再次提醒用戶,應用可以繼續運行。程序獲取Reachability對象之后,調用Reachability對象的startNotifier方法即可開啟該對象的被監聽狀態——當Reachability的連接狀態發生改變時,該對象將會發送一個kReachabilityChangedNotification通知給默認的通知中心,因此程序只要使用默認的通知中心監聽該通知即可。

      為了監聽網絡狀態的改變,在應用程序委托類(AppDelegate.m)的application: didFinishLaunchingWithOptions:方法中增加如下代碼:

    //使用通知中心監聽kReachabilityChangedNotification通知
    ? ?[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    ? ?//獲取訪問指定站點的Reachability對象
    ? ?Reachability *reach = [Reachability reachabilityWithHostName:@"www.cnblogs.com"];
    ? ?//讓Reachability對象開啟被監聽狀態
    ? ?[reach startNotifier];


    ? ? ? 上面的代碼使用默認的通知中心檢測kReachabilityChangedNotification通知,這意味著當Reachability的連接狀態發生改變時,默認的通知中心就會收到該通知,從而觸發應用程序委托類的reachabilityChanged:方法,還需要在應用程序委托類中定義如下方法:


    - (void) reachabilityChanged:(NSNotification*) note
    {
    ? ?//通過通知對象獲取被監聽的Reachability對象
    ? ?Reachability *curReach = [note object];
    ? ?//獲取Reachability對象的網絡狀態
    ? ?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:會判斷該Reachability對象的網絡連接狀態,當該對象的網絡連接狀態處于NotReachable時,程序會使用UIAlertView進行提醒。 



    轉載于:https://blog.51cto.com/8887511/1405996

    總結

    以上是生活随笔為你收集整理的检查网络状态的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。