导入头文件:#import "Reachability.h"

然后将 SystemConfiguration.framework 添加进工程:

1、检查当前的网络状态(wifi、WAN还是无网络)

NetworkEnvironment.h:

#import <Foundation/Foundation.h>
#import "Reachability.h" @interface NetworkEnvironment : NSObject /**
* @brief get the signalton engine object
* @return the engine object
*/
+ (NetworkEnvironment *)sharedInstance; /**
* @brief get the network statue
*/
- (BOOL)isNetworkReachable; /**
* @brief Judgment wifi is connected
*/
- (BOOL)isEnableWIFI; /**
* @brief To judge whether the 3G connection
*/
- (BOOL)isEnable3G; @end

NetworkEnvironment.m:

#import "NetworkEnvironment.h"
#import "Reachability.h" @interface NetworkEnvironment () @end @implementation NetworkEnvironment static NetworkEnvironment *g_instance = nil; - (id)init
{
self = [super init];
if (self) { }
return self;
} /**
* @brief Whether there are single instance
* @return the result
*/
+ (BOOL)sharedInstanceExists
{
return (nil != g_instance);
} /**
* @brief get the signalton engine object
* @return the engine object
*/
+ (NetworkEnvironment *)sharedInstance
{
@synchronized(self) {
if ( g_instance == nil ) {
g_instance = [[[self class] alloc] init];
//any other specail init as required
}
}
return g_instance;
} /**
* @brief get the network statue
*/
- (BOOL)isNetworkReachable
{
BOOL isReachable = NO;
Reachability *reachability = [Reachability reachabilityWithHostname:@"www.baidu.com"];
switch ([reachability currentReachabilityStatus]) {
case NotReachable:{
isReachable = NO;
}
break;
case ReachableViaWWAN:{
isReachable = YES;
}
break;
case ReachableViaWiFi:{
isReachable = YES;
}
break;
default:
isReachable = NO;
break;
}
return isReachable;
} /**
* @brief Judgment wifi is connected
*/
- (BOOL)isEnableWIFI
{
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
} /**
* @brief To judge whether the 3G connection
*/
- (BOOL)isEnable3G
{
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
} @end

调用方法:

#import "NetworkEnvironment.h"

if (NO == [[NetworkEnvironment sharedInstance] isNetworkReachable]) {
[WBCommonHelper showHUDWithText:@"网络状况异常"];

2、网络连接过程中实时监控网络状况(网络变化)

首先引入头文件:

#import "Reachability.h"

.h文件中定义

 Reachability *hostReach;

.m文件如下:

//wifi下自动更新,设置接受通知
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"UPDATESETTING"] isEqualToString:@"WIFI_AUTO"]) {
// 设置网络状态变化时的通知函数
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
hostReach = [[Reachability reachabilityWithHostname:@"www.baidu.com"] retain];
} #pragma mark - Public methods
-(void)reachabilityChanged:(NSNotification *)note
{
Reachability * curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[selfupdateInterfaceWithReachability:curReach];
} -(void)updateInterfaceWithReachability:(Reachability *)curReach
{
NetworkStatus status = [curReach currentReachabilityStatus];
//由其他环境变为wifi环境
if (status == ReachableViaWiFi)
{
NSLog(@"切换到WIFi环境");
}
}

Reachability.h中定义了三种网络状态:
    typedef enum {
        NotReachable = 0,            //无连接
        ReachableViaWiFi,            //使用3G/GPRS网络
        ReachableViaWWAN            //使用WiFi网络
    } NetworkStatus;

iOS开发 利用Reachability判断网络环境的更多相关文章

  1. ios利用Reachability确认网络环境3G/WIFI(转)

    iPhone开发技巧之网络篇(4)--- 确认网络环境  开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息.如果没有处理它们,是不会通过Apple的审查的. Apple 的 例程 Re ...

  2. ios开发利用AFN检测网络状态

    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager]; [manager setRe ...

  3. iOS中使用 Reachability 检测网络

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

  4. IOS开发中如何判断程序第一次启动(根据判断结果决定是否显示新手操作引导)

    IOS开发中如何判断程序第一次启动 在软件下载安装完成后,第一次启动往往需要显示一个新手操作引导,来告诉用户怎么操作这个app,这就需要在程序一开始运行就判断程序是否第一次启动,如果是,则显示新手操作 ...

  5. iOS开发-通过正则表达式判断字符串是否为纯阿拉伯数字

    iOS开发-通过正则表达式判断字符串是否为纯阿拉伯数字 简述:NSString * regex_0 = @"\\d{1,}";   /*允许首位为0*/ NSString * re ...

  6. IOS判断网络环境

    https://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html 我下载的是vertio ...

  7. 【开发记录】iOS中使用 Reachability 检测网络

    如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测. 写本文的目的 了解Re ...

  8. iOS中4种判断网络请求的方式(系统状态栏、AFNetworking、Reachability、自定义)

    iOS 实时判断网络状态 方法一:利用系统状态栏判断网络状态 // 状态栏是由当前app控制的,首先获取当前app UIApplication *app = [UIApplication shared ...

  9. iOS开发 - Swift实现检测网络连接状态及网络类型

    一.前言 在移动开发中,检测网络的连接状态尤其检测网络的类型尤为重要.本文将介绍在iOS开发中,如何使用Swift检测网络连接状态及网络类型(移动网络.Wifi). 二.如何实现 Reachabili ...

随机推荐

  1. 12月07日《奥威Power-BI智能分析报告制作方法 》腾讯课堂开课啦

            前几天跟我一个做报表的哥们聊天,听着他一茬一茬地诉苦:“每天做报表做到想吐,老板看报表时还是不给一个好脸色.”我也只能搬出那一套“过程大于结果”的内心疗程赠与他,没想到他反而怒了:“做 ...

  2. 如何安装mysql服务

    我刚开始安装mysql的时候,在windows的服务里面可以看到,但是装了以后有一段时间没有用它了,我在准备从windows的服务里面启动mysql服务的时候,发现没有mysql的服务了,那我的解决办 ...

  3. IntelliJ IDEA Community Edition 14.1.4下使用 Apache-Subversion搭建代码管理环境

    当前我的idea 版本是14.1.4. 1,)SVN Server下载与安装(https://www.visualsvn.com/server/): 因为我开发机是x64的,所以我优先下载 x64的 ...

  4. leetcode105:Construct Binary Tree from Preorder and Inorder Traversal

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...

  5. $.each()

    以下内容非原创 通过它,你可以遍历对象.数组的属性值并进行处理. 使用说明 each函数根据参数的类型实现的效果不完全一致: 1.遍历对象(有附加参数) $.each(Object, function ...

  6. Jenkins TcpSlaveAgentListener Config

    http://wenku.baidu.com/link?url=wDbeRoqh8ERRvBKXsKVi7biWe8e369iZmYTfEFDz0aI1Sj5YjXq_AN1gFjFjiS0yBw0W ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(31)-MVC使用RDL报表

    系列目录 这次我们来演示MVC3怎么显示RDL报表,坑爹的微软把MVC升级到5都木有良好的支持报表,让MVC在某些领域趋于短板 我们只能通过一些方式来使用rdl报表. Razor视图不支持asp.ne ...

  8. android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据

    为了实现这个功能可折腾了我好久,先上一份代码,经楼主验证是绝对可以用的而且也比较清晰的代码!(ps:还是先剧透下吧,第三方大部分浏览器无法成功.) 点击浏览器中的URL链接,启动特定的App. 首先做 ...

  9. ubuntu 中增加鼠标右键菜单,为Windows 的exe 程序快速增加桌面快捷键

    #!/bin/bashmyfile=$NAUTILUS_SCRIPT_SELECTED_FILE_PATHSmyfilename=${myfile##*/}myfilename=${myfilenam ...

  10. 遍历对象所有属性(json对象)

    var response = { "status": 1, "message": "\u6210\u529f", "data&qu ...