在網絡應用中,需要對用戶設備的網絡狀態進行實時監控,目的是
讓用戶了解自己的網絡狀態,防止一些誤會(比如怪應用無能)
根據用戶的網絡狀態進行智能處理,節省用戶流量,提高用戶體驗
WIFI\3G網絡:自動下載高清圖片
低速網絡:只下載縮略圖
沒有網絡:只顯示離線的緩存數據
蘋果官方提供了一個叫Reachability的示例程序,便於開發者檢測網絡狀態
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
Reachability的使用步驟
添加框架SystemConfiguration.framework
添加源代碼
包含頭文件
#import "Reachability.h"
常見用法
// 是否WIFI
+ (BOOL) IsEnableWIFI {
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
// 是否3G
+ (BOOL) IsEnable3G {
return ([[Reachability reachabilityForInternetConnectionen] currentReachabilityStatus] != NotReachable);
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
self.netReachability = [Reachability reachabilityForInternetConnection];
[self.netReachability startNotifier];
- (void)dealloc
{
[self.netReachability stopNotifier];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}
NetWorkTool工具類
#import
@interface NetworkTool : NSObject
/**
* 是否WIFI
*/
+ (BOOL)isEnableWIFI;
/**
* 是否3G
*/
+ (BOOL)isEnable3G;
@end
#import "NetWorkTool.h"
#import "Reachability.h"
@implementation NetworkTool
// 是否WIFI
+ (BOOL)isEnableWIFI {
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
// 是否3G
+ (BOOL)isEnable3G {
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}
@end
實現類
#import "ViewController.h"
#import "Reachability.h"
#import "NetworkTool.h"
@interface ViewController ()
@property (nonatomic, strong) Reachability *reachability;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 監聽網絡狀態發生改變的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
// 獲得Reachability對象
self.reachability = [Reachability reachabilityForInternetConnection];
// 開始監控網絡
[self.reachability startNotifier];
}
- (void)dealloc
{
[self.reachability stopNotifier];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)networkStateChange
{
NSLog(@"網絡狀態改變了");
[self checkNetworkState];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self checkNetworkState];
}
#pragma mark - App應用程序網絡類型改變就會調用
- (void)networkStateChange
{
if ([NetWorkTool isEnableWIFI]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示您" message:@"您現在網絡環境為wifi!開始暢享吧!" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alert show];
} else if ([NetWorkTool isEnable3G]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示您" message:@"您現在網絡環境為3G/4G網絡!" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alert show];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示您" message:@"您當前沒有可連的網絡或已經掉線!" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alert show];
}
}