#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. C++ map 使用erase在windows下崩溃,在linux下没有问题的原因

    注意:此程序在win环境下会出现上述描述的问题:在mac环境下第一种方式是正常运行的.Map.erase有3个重载函数: void erase(iterator position); size_typ ...

  2. IDEA配置

    关于IDEA的配置 配置注释模板 CTRL_SHIFT_S,在Live Templates中新增一个TemplateGroup,然后再新建两个模板,如下图: 新增cc-ClassComment /** ...

  3. Python播放、关闭音乐代码

    1.安装pygame:win + r :打开控制台输入:pip install pygame 2.#导入 import time import pygame 3.设置音乐绝对路径 #音乐路径 file ...

  4. 机器学习入门-BP神经网络模型及梯度下降法-2017年9月5日14:58:16

    BP(Back Propagation)网络是1985年由Rumelhart和McCelland为首的科学家小组提出,是一种按误差逆传播算法训练的多层前馈网络,是目前应用最广泛的神经网络模型之一. B ...

  5. Unity3D-RayMarch-几何图元0

    效果图: 将下面的shader代码对应的Material拖给一个面片,即可看到效果. shader代码: // Upgrade NOTE: replaced '_Object2World' with ...

  6. Pyhon入门基础(1)---Pycharm安装及破解

    一.下载安装 1.首先我们可以对比一下社区版和专业版的区别: 2.下载地址:https://www.jetbrains.com/pycharm/download/ 当我们开发的项目比较大的时候通常会涉 ...

  7. JSP 页面跳转的实现方法

    客户端跳转 1. 使用 href 超链接标记  <a href="new.jsp">跳转</a> 2. 使用表单提交完成跳转  <form actio ...

  8. 利用WMITool解决Windows10 浏览器主页被hao123劫持问题

    利用Windows10 激活工具KMS10_1025激活系统之后会出现首页 被劫持的问题 解决办法如下 1.下载wmi tool 连接地址 链接: https://pan.baidu.com/s/1g ...

  9. Asp.net Zero 应用实战-官方示例PhoneBook学习1_修改1版

    适用Zero版本:ASP.NET Core & Angular 2+ (aspnet-zero-core-3.1.0). 该版本官方有两个solution文件夹:Angular(前端) 和 a ...

  10. Qt自定义滚动条(不使用样式表)

    前面使用Qt 样式表实现滚动条,在实际工作中,发现存在一些瑕疵,例如如果在主窗口中绘制背景,则有可能给滚动条染色,还有如果想实现特殊的效果,则必须使用自定义风格,即从QStyle的子类派生出新的类型. ...