1.使用cocoapods配置第三方库
pod 'Reachability', '~> 3.2'
2.使用Reachability必须导入头文件
#import <Reachability/Reachability.h>
3.开始测试网络
1.测试当前网络的类型
NSLog(@"开启www.baidu.com网络检测");
Reachability *reach =[Reachability reachabilityWithHostname:@"www.baidu.com"];
NSLog(@"--current status: %@",reach.currentReachabilityString);
2.block块测试当前网络是否可用
reach.reachableBlock =^(Reachability *reachbi)
{
dispatch_async(dispatch_get_main_queue()
, ^{
NSLog(@"网络可用"); });
};
reach.unreachableBlock =^(Reachability *reachi)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"网络链接失败");
});
}
PS:监测网络状况可以使用 NotificationCenter