iOS-----檢測網路狀態,ios-----檢測網路
iOS不僅內建了豐富的網路通訊API來開發網路通訊應用,而且iOS平台還支援大量第三方網路通訊API,完全支援TCP、UDP網路通訊,
可以參考Apple的官方文檔學些使用CFNetWork進行UDP協議通訊的知識。
iOS平台上兩個第三方網路通訊架構:AFNetworking和ASIHTTPRequest,它們都對iOS網路通訊API進行了高度封裝,從而允許通過更
簡單的編程來實現網路通訊
檢測網路狀態檢查網路狀態
檢查裝置的網路狀態,需要如下兩個步驟。
1 |
下載、添加Reachability類 既可通過https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip連結下載Reachability.zip壓縮包,解壓該壓縮包將會得到一個Xcode項目 也可通過Xcode的幫組系統搜尋Reachability,接下來就可以在”Sample Code”分來中看到Reachability樣本項目,選擇”Reachability”清單項目,既可查看Reachability項目 單擊”Open Project” 按鈕,可以開啟該項目 |
2 |
為項目添加SystemConfiguration.framework架構 |
程式碼片段 |
1 ViewController.m 2 3 #import “ViewController.h” 4 5 #import “Reachability.h” 6 7 @implementation ViewController 8 9 - (void)viewDidLoad 10 11 { 12 13 [super viewDidLoad]; 14 15 } 16 17 18 19 // 測試網路狀態 20 21 - (IBAction)testNetStatus:(id)sender 22 23 { 24 25 NSString* site = self.siteFiled.text; 26 27 // 建立訪問指定網站的Reachability 28 29 Reachability* reach = [Reachability ReachabilityWithHostName: site]; 30 31 // 判斷該裝置的網路狀態 32 33 switch([reach currenReachabilityStatus]) 34 35 { 36 37 // 不能訪問 38 39 case NotReachable: 40 41 [self showAlert:[NSString stringWithFormat:@”不能訪問%@”,site]]; 42 43 break; 44 45 // 使用3G/4G網路 46 47 case ReachableViaWiFi: 48 49 [self showAlert:[NSString stringWithFormat:@”使用3G/4G網路訪問%@”,site]]; 50 51 break; 52 53 // 使用WiFi網路 54 55 case ReachableViaWWAN: 56 57 [self showAlert:[NSString stringWithFormat:@”使用3G/4G網路訪問%@”,site]]; 58 59 break; 60 61 } 62 63 } 64 65 66 67 // 測試WiFi 68 69 - (IBAction)testWifi:(id)sender 70 71 { 72 73 if([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable) 74 75 { 76 77 [self showAlert:@”WiFi 網路已經串連”]; 78 79 } 80 81 else 82 83 { 84 85 [self showAlert:@:”WiFi 網路不可用”]; 86 87 } 88 89 } 90 91 92 93 // 測試移動網路 94 95 - (IBAction)testInternet:(id)sender 96 97 { 98 99 if([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable)100 101 {102 103 [self showAlert:@”3G/4G網路已經串連”];104 105 }106 107 else108 109 {110 111 [self showAlert:@”3G/4G網路不可用”];112 113 }114 115 }116 117 - (void)showAlert:(NSString*)msg118 119 {120 121 UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@”網路狀態” message:msg delegate:nil cancelButtonTitle:@”確定” otherButtonTitles:nil];122 123 [alert show];124 125 }126 127 @end |
說明 |
上面程式首先調用了Reachability類的reachabilityWithHostName:類方法來擷取Reachability對象,然後調用該對象的currentReachabilityStatus方法來擷取訪問指定網站的方式,該方法返回NetworkStatus枚舉值,該枚舉值一共有如下3個: typedef enum{ NotReachable = 0,// 無串連 ReachableViaWiFi,// 使用3G/4G網路 ReachableViaWWAN // 使用WiFi } NetworkStatus; 因此,上面程式對Reachability的currentReachabilityStatus方法傳回值進行判斷,這樣即可擷取該應用訪問網路的狀態與方式 |
監聽網路狀態改變
程式擷取Reachability對象之後,調用Reachability對象的startNotifier方法即可開啟該對象的被監聽狀態-------當Reachability的
連續狀態發生改變時,該對象將會發送一個kReachabilityChangedNotification通知給預設的通知中樞,因此程式只要使用預設的通知中
心監聽該通知即可.
為了監聽網路狀態的改變,在應用程式委託類的application:didFinishLaunchingWithOptions:方法中增加如下一段代碼.
程式碼片段 |
AppDelegate.m// 使用通知中樞監聽kReachabilityChangedNotification通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)name:kReachabilityChangedNotificationobject:nil];// 擷取訪問指定網站的Reachability對象Reachability* reach = [Reachability reachabilityWithHostName:@”www.baidu.com”];// 讓Reachability對象開啟被監聽狀態[reach startNotifier]; |
說明 |
上面的代碼使用預設的通知中樞檢測kReachabilityChangedNotification通知,這意味著當Reachability的串連狀態發生改變時,預設的通知中樞就會收到該通知,從而觸發應用程式委託類的reachabilityChanged:方法,因此還需要在應用程式委託類中定義如下方法. |
程式碼片段 |
1 AppDelegate.m 2 3 - (void)reachabilityChanged:(NSNotification *)noto 4 5 { 6 7 // 通過通知對象擷取被監聽的Reachability對象 8 9 Reachability *curReach = [note object];10 11 // 擷取Reachability對象的網路狀態12 13 NetworkStatus status = [curReach currentReachabilityStatus];14 15 if(status == NotReachable)16 17 {18 19 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”提 醒” message:@”不能訪問www.baidu.com” delegate:nil cancelButtonTitle:@”YES” otherButtonTitles:nil];20 21 [alert show];22 23 }24 25 } |
說明 |
從上面的代碼可以看出,該樣本會對訪問www.baidu.com的Reachability對象進行監聽,當該對象狀態處於NotReachable時,程式會使用UIAlertView進行提醒. |