一、說(shuō)明
在網(wǎng)絡(luò)應(yīng)用中,需要對(duì)用戶設(shè)備的網(wǎng)絡(luò)狀態(tài)進(jìn)行實(shí)時(shí)監(jiān)控,有兩個(gè)目的:
(1)讓用戶了解自己的網(wǎng)絡(luò)狀態(tài),防止一些誤會(huì)(比如怪應(yīng)用無(wú)能)
(2)根據(jù)用戶的網(wǎng)絡(luò)狀態(tài)進(jìn)行智能處理,節(jié)省用戶流量,提高用戶體驗(yàn)
WIFI\3G網(wǎng)絡(luò):自動(dòng)下載高清圖片
低速網(wǎng)絡(luò):只下載縮略圖
沒(méi)有網(wǎng)絡(luò):只顯示離線的緩存數(shù)據(jù)
Reachability 類中定義了三種網(wǎng)絡(luò)狀態(tài):
>typedef Num{
NotReachable = 0, ?//無(wú)連接
ReachableViaWiFi, ?//使用3G/GPRS網(wǎng)絡(luò)
ReachableViaWWAN? ?//使用WiFi網(wǎng)絡(luò)
}NetworkStatus;
蘋果官方提供了一個(gè)叫Reachability的示例程序,便于開發(fā)者檢測(cè)網(wǎng)絡(luò)狀態(tài)
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
二、監(jiān)測(cè)網(wǎng)絡(luò)狀態(tài)
Reachability的使用步驟
添加框架SystemConfiguration.framework
添加源代碼
示例:
?//比如檢測(cè)某一特定站點(diǎn)的接續(xù)狀況,可以使用下面的代碼
Reachability *reachability = [Reachablity ?reachabilityWithHostName:@"www.baidu.com"];
switch([reachabilityStatus]){
case??NotReachable:
break;
case ?ReachableViaWiFi:
break;
case ?ReachableViaWWAN:
break;
}
4.檢查當(dāng)前網(wǎng)絡(luò)環(huán)境
//程序啟動(dòng)時(shí),如果想檢測(cè)可用的網(wǎng)絡(luò)環(huán)境,可以像這樣來(lái)使用
//是否wifi
+ (BOOL)isEnableWIFI
{
return ([[Reachability reachabiliyForLocalWIFI] currentReachabilityStatus] != NotReachable);
}
//是否3G
+ (BOOL)isEnable3G
{
return ([[Reachability reachabiliyForInternetConnetion] currentReachabilityStatus] != NotReachable);
}
連接狀態(tài)實(shí)時(shí)通知
網(wǎng)絡(luò)連接狀態(tài)的實(shí)時(shí)檢查,通知在網(wǎng)絡(luò)應(yīng)用中也是十分必要的。接續(xù)狀態(tài)發(fā)生變化時(shí),需要及時(shí)地通知用戶。由于Reachability1.5版與2.0版有一些變化,這里分開來(lái)說(shuō)明使用方法。
Reachability 1.5
// My.AppDelegate.h
#import "Reachability.h"
@interface MyAppDelegate : NSObject{
???? NetworkStatus remoteHostStatus;
}
@property NetworkStatus remoteHostStatus;
@end
// My.AppDelegate.m
#import "MyAppDelegate.h"
@implementation MyAppDelegate
@synthesize remoteHostStatus;
// 更新網(wǎng)絡(luò)狀態(tài)
- (void)updateStatus {
self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
}
// 通知網(wǎng)絡(luò)狀態(tài)
- (void)reachabilityChanged:(NSNotification *)note {
[self updateStatus];
if (self.remoteHostStatus == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil) message:NSLocalizedString(@"NotReachable", nil)
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
// 程序啟動(dòng)器,啟動(dòng)網(wǎng)絡(luò)監(jiān)視
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// 設(shè)置網(wǎng)絡(luò)檢測(cè)的站點(diǎn)
[[Reachability sharedReachability] setHostName:@"www.apple.com"];
[[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
// 設(shè)置網(wǎng)絡(luò)狀態(tài)變化時(shí)的通知函數(shù)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
name:@"kNetworkReachabilityChangedNotification" object:nil];
[self updateStatus];
}
- (void)dealloc {
// 刪除通知對(duì)象
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Reachability 2.0
// MyAppDelegate.h
@class Reachability;
@interface MyAppDelegate : NSObject{
Reachability? *hostReach;
}
@end
// MyAppDelegate.m
- (void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus status = [curReach currentReachabilityStatus];
if (status == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""
message:@"NotReachable"
delegate:nil
cancelButtonTitle:@"YES" otherButtonTitles:nil];
[alert show];
}
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// ...
// 監(jiān)測(cè)網(wǎng)絡(luò)情況
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
[hostReach startNotifer];
}