ios之好用的Reachability
#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的更多相关文章
- iOS开发之CocoaPods的安装与使用
前言部分 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods) 可以用来方便的统一管理这些第三方库. 一.安装 由 ...
- iOS开发 cocoapods的安装以及使用
一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库(从一个坑出来,又 ...
- iOS网络高级编程:iPhone和iPad的企业应用开发之错误处理
本章内容 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWluZ2h1YXdlbmthbmc=/font/5a6L5L2T/fontsize/400/fi ...
- CocoaPods的安装、使用、以及遇到的问题
CocoaPods是什么? 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而 ...
- Podfile使用说明
什么是Podfile ? CocoaPods是用ruby实现的,因此Podfile文件的语法就是ruby的语法.podfile是一个说明文件,用以描述管理一个或者多个Xcode project的tar ...
- CocoaPods安装、卸载、使用说明(Mac ox 10.11+)
一.全新安装前,先检查是否有安装残留 由于Mac 10.11更改了安全机制,所以cocoapods得安装和卸载命令也有所改变, 1.如果之前装过cocopods,最好先卸载掉,卸载命令: $ sudo ...
- IOS开发之网络编程开源类 Reachability应用
先看Reachability.h发现 #import <Foundation/Foundation.h> #import <SystemConfiguration/SystemCon ...
- iOS中使用 Reachability 检测网络
iOS中使用 Reachability 检测网络 内容提示:下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测. 写本文的目的 了解Reachability都 ...
- iOS开发网络篇—Reachability检测网络状态
前言:当应用程序需要访问网络的时候,它首先应该检查设备的网络状态,确认设备的网络环境及连接情况,并针对这些情况提醒用户做出相应的处理.最好能监听设备的网络状态的改变,当设备网络状态连接.断开时,程序也 ...
随机推荐
- 基于bootstrap的datepicker
<script src="<%=path %>/js/bootstrap-datepicker.min.js"/> <script src=" ...
- Codeforces Round #491 (Div. 2)
Codeforces Round #491 (Div. 2) https://codeforces.com/contest/991 A #include<bits/stdc++.h> us ...
- swift 移除控制器中的已经存在的VC 注意
1.要用 || 2. 如果使用这个代码 只会移除一个VC
- git log命令常用参数集合
git log 查看 提交历史 默认不用任何参数的话,git log 会按提交时间列出所有的更新,最近的更新排在最上面. 常用的格式占位符写法及其代表的意义.选项 说明%H 提交对象(commit)的 ...
- web自动化上传附件 2
当我们进行某一项web自动化脚本编写时,有上传附件操作,点击附件直接打开了windows窗口,而有的点击添加附件打开一个小窗体,再点击‘浏览’才打开windows窗口, 中间多了这么一个小窗体的操作, ...
- jmeter手写脚本,使用正则获取cookie(禁用cookies管理器)
注:这里以bugfree为例 1.bugfree登录时会有重定向,这会导致每个URL都会有.因此要手动获取cookie的时候,需要去掉重定向勾选 正则获取动态PHPsession 获取到值后,放到信息 ...
- node.js生成二维码
var http = require('http'); var qs = require('querystring'); var qrImg = require('qr-image'); var se ...
- nginx集成环境下载
https://visual-nmp.en.softonic.com/download
- 推荐一款Notepad++主题Dracula
https://draculatheme.com/notepad-plus-plus/ Activating theme Go to %AppData%\Notepad++\themes Place ...
- spring切换多数据源
应用场景:在一个项目需要用到两个或两个以上的数据库时,要进行切换数据库,来操作相应的表. 框架:用的是spring 的org.springframework.jdbc.datasource.looku ...