一喻圃、說明
在網(wǎng)絡(luò)應(yīng)用中,需要對用戶設(shè)備的網(wǎng)絡(luò)狀態(tài)進行實時監(jiān)控,有兩個目的:
(1)讓用戶了解自己的網(wǎng)絡(luò)狀態(tài)涮坐,防止一些誤會(比如怪應(yīng)用無能)
(2)根據(jù)用戶的網(wǎng)絡(luò)狀態(tài)進行智能處理焚辅,節(jié)省用戶流量映屋,提高用戶體驗
WIFI\3G網(wǎng)絡(luò):自動下載高清圖片
低速網(wǎng)絡(luò):只下載縮略圖
沒有網(wǎng)絡(luò):只顯示離線的緩存數(shù)據(jù)
Reachability 類中定義了三種網(wǎng)絡(luò)狀態(tài):
>typedef Num{
NotReachable = 0, ?//無連接
ReachableViaWiFi, ?//使用3G/GPRS網(wǎng)絡(luò)
ReachableViaWWAN? ?//使用WiFi網(wǎng)絡(luò)
}NetworkStatus;
蘋果官方提供了一個叫Reachability的示例程序,便于開發(fā)者檢測網(wǎng)絡(luò)狀態(tài)
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
二同蜻、監(jiān)測網(wǎng)絡(luò)狀態(tài)
Reachability的使用步驟
添加框架SystemConfiguration.framework
添加源代碼
示例:
?//比如檢測某一特定站點的接續(xù)狀況棚点,可以使用下面的代碼
Reachability *reachability = [Reachablity ?reachabilityWithHostName:@"www.baidu.com"];
switch([reachabilityStatus]){
case??NotReachable:
break;
case ?ReachableViaWiFi:
break;
case ?ReachableViaWWAN:
break;
}
4.檢查當前網(wǎng)絡(luò)環(huán)境
//程序啟動時,如果想檢測可用的網(wǎng)絡(luò)環(huán)境湾蔓,可以像這樣來使用
//是否wifi
+ (BOOL)isEnableWIFI
{
return ([[Reachability reachabiliyForLocalWIFI] currentReachabilityStatus] != NotReachable);
}
//是否3G
+ (BOOL)isEnable3G
{
return ([[Reachability reachabiliyForInternetConnetion] currentReachabilityStatus] != NotReachable);
}
連接狀態(tài)實時通知
網(wǎng)絡(luò)連接狀態(tài)的實時檢查瘫析,通知在網(wǎng)絡(luò)應(yīng)用中也是十分必要的。接續(xù)狀態(tài)發(fā)生變化時默责,需要及時地通知用戶贬循。由于Reachability1.5版與2.0版有一些變化,這里分開來說明使用方法桃序。
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];
}
}
// 程序啟動器杖虾,啟動網(wǎng)絡(luò)監(jiān)視
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// 設(shè)置網(wǎng)絡(luò)檢測的站點
[[Reachability sharedReachability] setHostName:@"www.apple.com"];
[[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
// 設(shè)置網(wǎng)絡(luò)狀態(tài)變化時的通知函數(shù)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
name:@"kNetworkReachabilityChangedNotification" object:nil];
[self updateStatus];
}
- (void)dealloc {
// 刪除通知對象
[[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)測網(wǎng)絡(luò)情況
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
[hostReach startNotifer];
}