Reachability
使用Reachability框架
AFN封裝了Reachability涩嚣,可以用AFNetworkReachabilityManager來監(jiān)測(cè)
Reachability描述
//發(fā)生網(wǎng)絡(luò)狀態(tài)發(fā)生的通知
extern NSString *const kReachabilityChangedNotification;
typedef NS_ENUM(NSInteger, NetworkStatus) {
// Apple NetworkStatus Compatible Names.
NotReachable = 0,//沒網(wǎng)
ReachableViaWiFi = 2,//wifi
ReachableViaWWAN = 1//流量
};
@class Reachability;
typedef void (^NetworkReachable)(Reachability * reachability);
typedef void (^NetworkUnreachable)(Reachability * reachability);
@interface Reachability : NSObject
@property (nonatomic, copy) NetworkReachable reachableBlock;
@property (nonatomic, copy) NetworkUnreachable unreachableBlock;
@property (nonatomic, assign) BOOL reachableOnWWAN;
+(Reachability*)reachabilityWithHostname:(NSString*)hostname;
// This is identical to the function above, but is here to maintain
//compatibility with Apples original code. (see .m)
//檢查能否連接到指定的主機(jī)
+(Reachability*)reachabilityWithHostName:(NSString*)hostname;
//監(jiān)測(cè)默認(rèn)路由是否可用
+(Reachability*)reachabilityForInternetConnection;
//監(jiān)測(cè)能否連接到指定的ip
+(Reachability*)reachabilityWithAddress:(void *)hostAddress;
//監(jiān)測(cè)本地wifi是否可用
+(Reachability*)reachabilityForLocalWiFi;
-(Reachability *)initWithReachabilityRef:(SCNetworkReachabilityRef)ref;
//開始在當(dāng)前環(huán)境運(yùn)行循環(huán)監(jiān)聽網(wǎng)絡(luò)狀態(tài)通知
-(BOOL)startNotifier;
-(void)stopNotifier;
-(BOOL)isReachable;
-(BOOL)isReachableViaWWAN;
-(BOOL)isReachableViaWiFi;
// WWAN may be available, but not active until a connection has been established.
// WiFi may require a connection for VPN on Demand.
-(BOOL)isConnectionRequired; // Identical DDG variant.
-(BOOL)connectionRequired; // Apple's routine.
// Dynamic, on demand connection?
-(BOOL)isConnectionOnDemand;
// Is user intervention required?
-(BOOL)isInterventionRequired;
//當(dāng)前的網(wǎng)絡(luò)狀態(tài)
-(NetworkStatus)currentReachabilityStatus;
-(SCNetworkReachabilityFlags)reachabilityFlags;
-(NSString*)currentReachabilityString;
-(NSString*)currentReachabilityFlags;
實(shí)時(shí)監(jiān)聽網(wǎng)絡(luò)狀態(tài)
@interface ViewController ()
@property (nonatomic, strong)Reachability* rgManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkStatu:) name:kReachabilityChangedNotification object:nil];
[self.rgManager startNotifier];
}
- (void)checkStatu:(NSNotification*)not{
switch (self.rgManager.currentReachabilityStatus) {
case NotReachable:
NSLog(@"沒有網(wǎng)絡(luò)");
break;
case ReachableViaWiFi:
NSLog(@"wifi");
break;
case ReachableViaWWAN:
NSLog(@"3g");
break;
default:
break;
}
}
- (void)dealloc{
[self.rgManager stopNotifier];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (Reachability *)rgManager{
if (!_rgManager) {
//1.找一個(gè)基本不會(huì)掛的服務(wù)器
//2.自己的服務(wù)器
_rgManager = [Reachability reachabilityWithHostName:@"baidu.com"];;
}
return _rgManager;
}