1.cocoapods导入RealReachability
2.使用
oc需要的文件中导入
#import <RealReachability/RealReachability.h>
swift需要的地方导入
import RealReachability
3.打开监控
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
[[RealReachability sharedInstance] startNotifier];
return YES;
}
4.在当前页面添加观察者
//在具体的页面添加观察者(实时监测网络的变化)
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkstate:) name:kRealReachabilityChangedNotification object:nil];
}
//观察者实时检测方法
- (void)netWorkstate:(NSNotification *)notice {
RealReachability *reachability = (RealReachability *)notice.object;
//检测网络
NSInteger status = [reachability currentReachabilityStatus];
[self notNetPrompt:status];
}
5.每次进当前页面都检测下网络状态
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//检测网络
NSInteger status = [[RealReachability sharedInstance] currentReachabilityStatus];
[self notNetPrompt:status];
}