#import <Foundation/Foundation.h>

@interface NetWorkTool : NSObject

+ (instancetype)shareInstance;

- (void)startListing;

@end

#import "NetWorkTool.h"

@import Reachability;

@interface NetWorkTool()

@property (nonatomic) Reachability *hostReachability;

@property (nonatomic) Reachability *internetReachability;

@end

@implementation NetWorkTool

static NetWorkTool *tool = nil;

+(instancetype)shareInstance

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

tool = [[NetWorkTool alloc]init];

});

return tool;

}

- (void)startListing

{

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

NSString *remoteHostName = @"www.apple.com";

self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];

[self.hostReachability startNotifier];

[self updateInterfaceWithReachability:self.hostReachability];

self.internetReachability = [Reachability reachabilityForInternetConnection];

[self.internetReachability startNotifier];

[self updateInterfaceWithReachability:self.internetReachability];

}

- (void) reachabilityChanged:(NSNotification *)note

{

Reachability* curReach = [note object];

NSParameterAssert([curReach isKindOfClass:[Reachability class]]);

[self updateInterfaceWithReachability:curReach];

}

- (void)updateInterfaceWithReachability:(Reachability *)reachability

{

if (reachability == self.internetReachability)

{

[self configureReachability:reachability];

}

}

- (void)configureReachability:(Reachability *)reachability

{

NetworkStatus netStatus = [reachability currentReachabilityStatus];

BOOL connectionRequired = [reachability connectionRequired];

switch (netStatus)

{

case NotReachable:        {

NSLog(@"没有网络");

connectionRequired = NO;

break;

}

case ReachableViaWWAN:        {

NSLog(@"手机自带网络");

break;

}

case ReachableViaWiFi:        {

NSLog(@"wifi网络");

break;

}

}

}

- (void)dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];

}

@end

#define NetWorkManager   [NetWorkTool shareInstance]

[NetWorkManager startListing];

didFinishLaunchingWithOptions中调用即可 全局可以监听

ios之好用的Reachability的更多相关文章

  1. iOS开发之CocoaPods的安装与使用

    前言部分 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods) 可以用来方便的统一管理这些第三方库. 一.安装 由 ...

  2. iOS开发 cocoapods的安装以及使用

    一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库(从一个坑出来,又 ...

  3. iOS网络高级编程:iPhone和iPad的企业应用开发之错误处理

    本章内容 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWluZ2h1YXdlbmthbmc=/font/5a6L5L2T/fontsize/400/fi ...

  4. CocoaPods的安装、使用、以及遇到的问题

    CocoaPods是什么? 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而 ...

  5. Podfile使用说明

    什么是Podfile ? CocoaPods是用ruby实现的,因此Podfile文件的语法就是ruby的语法.podfile是一个说明文件,用以描述管理一个或者多个Xcode project的tar ...

  6. CocoaPods安装、卸载、使用说明(Mac ox 10.11+)

    一.全新安装前,先检查是否有安装残留 由于Mac 10.11更改了安全机制,所以cocoapods得安装和卸载命令也有所改变, 1.如果之前装过cocopods,最好先卸载掉,卸载命令: $ sudo ...

  7. IOS开发之网络编程开源类 Reachability应用

    先看Reachability.h发现 #import <Foundation/Foundation.h> #import <SystemConfiguration/SystemCon ...

  8. iOS中使用 Reachability 检测网络

    iOS中使用 Reachability 检测网络 内容提示:下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测.   写本文的目的 了解Reachability都 ...

  9. iOS开发网络篇—Reachability检测网络状态

    前言:当应用程序需要访问网络的时候,它首先应该检查设备的网络状态,确认设备的网络环境及连接情况,并针对这些情况提醒用户做出相应的处理.最好能监听设备的网络状态的改变,当设备网络状态连接.断开时,程序也 ...

随机推荐

  1. 使用rke快速安装K8s集群

    操作系统 centos 7.5 yum update -y yum install docker -y 关闭防火墙.selinux 下载rke helm https://github.com/helm ...

  2. 【转】Webdriver的PageObject改造By 张飞

    Webdriver的PageObject改造 PageObject中提供了一个@FindBy注解,也非常好用,但由于其是一次性全部初始化所有的WebElement,对于当前还不存在于页面上的Eleme ...

  3. 写给自己的小CASE

    一时的完美不代表什么,重要的是不断地学习和成长:直面挑战,不断进取.   摘自<看见成长的自己>.

  4. ListBox设置背景色无效的问题。 listview类似

    <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"&g ...

  5. 避免切换横竖屏Fragment的重复加载导致UI混乱

    当我们切换横竖屏时 Activity的生命周期就会重走一遍,自然 其中的Fragment的生命周期也就重新走了一遍,实践证明 当熄屏 再开屏时 Fragment的生命周期也会重走一遍 解决方案: an ...

  6. wpf 加载资源文件

    方法一:App.xaml页面上写 <Application x:Class="LanguageChange.App" xmlns="http://schemas.m ...

  7. mongocxx-driver编译安装

    1. 确保安装epel yum install -y epel-release 2. 按照<CentOS7.2部署node-mapnik>一文中的步骤,手动安装 gcc-6.2.0 和 b ...

  8. gk888t打印机安装

    https://jingyan.baidu.com/article/948f5924090c7ad80ff5f9c5.html

  9. IDEA开发环境中快捷键与系统 QQ等冲突的解决办法

    1.快捷键冲突1:IDEA中,Ctrl+Alt+向左/右/箭头快捷键 (回到光标的前一个位置,回到光标的后一个位置).该快捷键与系统中旋转屏幕的快捷键冲突了,需要解决.为了保留IDEA的中快捷键,我就 ...

  10. Collection<T> 的一个坑

    当前所在的公司偏好使用 Collection<T>(System.Collections.ObjectModel), 这货比起List<T>不仅少了很多实用方法, 而且还有一个 ...