#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. linux应用之test命令详细解析

    test命令用法. 功能:检查文件和比较值 1)判断表达式 if test  (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2                  两个表 ...

  2. django RESTful设计方法

    1. 域名 应该尽量将API部署在专用域名之下. https://api.example.com 如果确定API很简单,不会有进一步扩展,可以考虑放在主域名下. https://example.org ...

  3. flutter Dialog里ListView的问题

    showDialog( context: context, builder: (ctx) { return // Dialog( // child: Container( // padding: Ed ...

  4. Ubuntu 16.04 上安装 PCL 1.8.0

    Ubuntu16.04之后安装pcl可以直接apt-get sudo apt-get install libpcl-dev pcl-tools 安装之前,准备一些依赖库 sudo apt-get up ...

  5. Python基础-python流程控制之循环结构(五)

    循环结构 循环结构可以减少源程序重复书写的代码量,用来描述重复执行某段算法的问题. Python中循环结构分为两类,分别是 while 和 for .. in. 一.while循环 格式1: whil ...

  6. 如何用命令将本地项目上传到git[z]

    1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...

  7. python3下获取全局坐标

    前段时间自己想用python写一个关于截屏翻译的小功能,所以首先需要获取鼠标的坐标动作有按下.拖拽.释放.查找了很多资料无非是pyhook或者用其他语言重写.但是自己用的是python3.65,第三方 ...

  8. Python读取excel表的数据

    from openpyxl.reader.excel import load_workbook #读取xlsx文件def readExcelFile(path): dic={} #打开文件 file= ...

  9. scrapy 爬取前程无忧

    spider # -*- coding: utf-8 -*- import scrapy from Jobs.items import JobsItem class Job51spiderSpider ...

  10. oracle自带总页数分页sql

    string strSQL = string.Format(@"select * from( with temp as (select * from * where {0} order by ...