#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. Tensorflow学习笔记2019.01.03

    tensorflow学习笔记: 3.2 Tensorflow中定义数据流图 张量知识矩阵的一个超集. 超集:如果一个集合S2中的每一个元素都在集合S1中,且集合S1中可能包含S2中没有的元素,则集合S ...

  2. 如何修复“网络路径”,错误代码0x80070035

    1.以管理员权限 运行 cmd. sc.exe config lanmanworkstation depend= bowser/mrxsmb10/nsi回车sc.exe config mrxsmb20 ...

  3. R语言如何读取.csv文件

    以下是我关于如何在R语言中读取.csv文件及一些需要注意的细节的总结,希望能帮助到大家~

  4. Java并发编程之同步

    1.synchronized 关键字 synchronized 锁什么?锁对象. 可能锁对象包括: this, 临界资源对象,Class 类对象. 1.1 同步方法 synchronized T me ...

  5. angular $digest 运行10次货10次以上会抛出异常

    今天在做项目时,遇到一个问题,红圈处输入其他数字(n多次)也不会报异常,但是有一种特例,即0,1,0,1,0,1这种顺序输入时,输入第13次时,页面计算结果(蓝色圆圈)不会更新,困扰了1天多这个问题, ...

  6. 现代编译原理——第六章:中间树 IR Tree 含源码

    转自: http://www.cnblogs.com/BlackWalnut/p/4559717.html 这一章,就虎书而言,理论知识点是及其少的,就介绍了为什么要有一个中间表示树.看下面这张图就能 ...

  7. Convolutional LSTM Network: A Machine LearningApproach for Precipitation Nowcasting

    Convolutional LSTM Network: A Machine LearningApproach for Precipitation Nowcasting 这篇文章主要是了解方法. 原始文 ...

  8. 常用Linux VPS/服务器SSH连接工具 - Xshell下载与使用

    我们很多网友可能初次接触Linux VPS.服务器,所以在购买完毕VPS主机不知道如何登录.有些网友甚至直接类似WIN系统一样直接在桌面远程连接工具连接,可想而知肯定是无法连接的.因为如果我们购买的是 ...

  9. rpm --rebuilddb

    rpm -ivh .....rpm 报Bus Error rpm --rebuilddb

  10. rest接口webservice接口利用http请求方式发送数据

    所需依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>h ...