网络状态检测Reachability
2018-07-20 来源:open-open
#import "Reachability.h"
@interface JRViewController ()<UIActionSheetDelegate>
@property(nonatomic,strong) Reachability * reach;
@end
@implementation JRViewController
- (void)viewDidLoad {
[super viewDidLoad];
//监听Reachability.h————————————————————————————————————————————————————————————————
//
[self monitorNetState:nil];
// 开启通知监控,实时等待
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(monitorNetState:) name:kReachabilityChangedNotification object:nil];//object是指监听谁发出的通知,在这里可以写self.reach,也可以写nil
[self.reach startNotifier];//一定要写开始监控
}
//**********************************************
//reachability监听————————————————————————————————————————————————————————————————
- (void) monitorNetState:(NSNotification *)noti
{
NSLog(@"%@", noti);
if(self.reach==nil)//
{
self.reach=[Reachability reachabilityForInternetConnection];//不是单例
//他还有两个子类
// ReachableViaWiFi;
// ReachableViaWWAN;
}
if(self.reach.currentReachabilityStatus!=NotReachable)
{
if (self.reach.currentReachabilityStatus==ReachableViaWiFi)
{
NSLog(@"wifi");
}
else
{
NSLog(@"3g/2g");
}
}
else
{
NSLog(@"没有网");
}
}
//移除监听
-(void)dealloc{
//reachability需要自己停止
[self.reach stopNotifier];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐