1.cocoapods導(dǎo)入RealReachability
2.使用
oc需要的文件中導(dǎo)入
#import <RealReachability/RealReachability.h>
swift需要的地方導(dǎo)入
import RealReachability
3.打開(kāi)監(jiān)控
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
[[RealReachability sharedInstance] startNotifier];
return YES;
}
4.在當(dāng)前頁(yè)面添加觀(guān)察者
//在具體的頁(yè)面添加觀(guān)察者(實(shí)時(shí)監(jiān)測(cè)網(wǎng)絡(luò)的變化)
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkstate:) name:kRealReachabilityChangedNotification object:nil];
}
//觀(guān)察者實(shí)時(shí)檢測(cè)方法
- (void)netWorkstate:(NSNotification *)notice {
RealReachability *reachability = (RealReachability *)notice.object;
//檢測(cè)網(wǎng)絡(luò)
NSInteger status = [reachability currentReachabilityStatus];
[self notNetPrompt:status];
}
5.每次進(jìn)當(dāng)前頁(yè)面都檢測(cè)下網(wǎng)絡(luò)狀態(tài)
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//檢測(cè)網(wǎng)絡(luò)
NSInteger status = [[RealReachability sharedInstance] currentReachabilityStatus];
[self notNetPrompt:status];
}