1. 在SystemConfiguration.famework中提供和联网相关的function, 可用来检查网络连接状态。

2. SC(SystemConfiguration)框架中关于测试连接网络状态相关的函数定义在SCNetworkReachability.h文件中,主要函数如下:

// 创建测试连接的引用
SCNetworkReachabilityRef SCNetworkReachabilityCreateWithAddress(CFAllocatorRef allocator, const struct sockaddr *address);
// 根据传入的地址测试连接, 第一参数可以为NULL或kCFAllocatorDefault, 第二参数为需要测试连接的IP地址,当为0.0.0.0时则可以查询本机的网络连接状态, 同时返回一个引用必须在用完后释放。
SCNetworkReachabilityRef SCNetworkReachabilityCreateWithName(CFAllocatorRef allocator, const char *nodename);
// 根据传入的网址测试连接,第二参数如"www.apple.com",其他与上同 
// 确定连接的状态
Boolean SCNetworkReachabilityGetFlags(SCNetworkReachability target, SCNetworkReachabilityFlags *flags);
// 用来获得测试连接的状态,第一参数为之前建立的测试连接引用,第二参数用来保存获得的状态,如果获得状态则返回TRUE, 否则返回FALSE

主要的数据类型如下:

SCNetworkReachabilityRef : 用来保存创建测试连接返回的引用

主要常量如下:

Network Reachability Flags:

Flags that indicate the reachability of a network node name or address, including whether a connction is required, and whether some user intervention might by required when eatablishing a connection.

标识(Flags)代表对一个域名(网络结点)或者地址(IP)的可连接性, 其包括是否需要一个网络连接以及在建立网络连接的过程中是否需要用户干预。

<span style="">kSCNetworkReachabilityFlagsIsWWAN </span>:
 
The specified node name or address can be reached via a cellular connection, such ad EDGE or GPRS.
 
通过EDGE(2G到3G的过渡技术方案,这里可以理解为3G)或者GPRS(2G)连接到指定域名或地址。<br><br>
<span style="">kSCNetworkReachabilityFlagsReachable</span> :<br>
The specified node name or address can be reached using the current network configuration.<br>
通过当前的网络配置可连接到指定的域名和地址。<br>
<span style="">kSCNetworkReachabilityFlagsTransientConnection</span> :<br>
The specified node name or address can be reached via a transient connection, such as PPP.<br>
通过一个短暂的(网络)连接可以到达指定的域名或地址,比如PPP(Point to Point Protocol)协议。<br>
<span style=""> kSCNetworkReachabilityFlagsConnectionRequired</span> : <br>
The specified node name or address can be reached using the current network configuration, but a connection must first be
established. If this flag is set, the kSCNetworkReachabilityFlagsConnectionOnTraffic flag, kSCNetworkReachabilityFlagsConnectionOnDemand flag, or kSCNetworkReachabilityFlagsIsWWAN flag is also typically set to indicate the type of connection required. If the user must manually make the connection, the kSCNetworkReachabilityFlagsInterventionRequired flag is also set.<br><br>可通过当前的网络配置连接到指定的域名或地址,但首先必须建立一个网络连接。如果此标识(kSCNetworkReachabilityFlagsConnectionRequired)被设定,那么标识kSCNetworkReachabilityFlagsConnectionOnTraffic, kSCNetworkReachabilityFlagsConnectionOnDemand或者kSCNetworkabilityFlagsIsWWAN通常应被设定为指定的网络连接要求类型。如果用户必须手动生成此连接, 那么kSCNetworkReachabilityFlagsInterventionRequired标识也应要设定。<br><br>
<span style="">kSCNetworkReachabilityFlagsConnectionOnTraffic</span> :
 
The specified node name or address can be reached using the current network configuration, but a connection must first be established.
Any traffic directed to the specified name or address will initiate the connection.<br><br>可通过当前网络配置连接到指定的域名或地址,但首先必须建立一个网络连接,任何到达指定域名或地址的连接都将始于此连接。<br><br>
<span style="">kSCNetworkReachabilityFlagsInterventionRequired</span> :
 
The specified node name or address can be reached using the current network configuration, but a connection must first be established.<br><br>可通过当前网络配置连接到指定的域名或地址,但首先必须建立一个网络连接。<br><br>
<span style="">kSCNetworkReachabilityFlagsConnectioniOnDemand</span> :
 
The specified node name or address can be reached using using the current network configuration, but a connection must first be established. <br>The connection will be established "On Demand" by the CFSocketStream programming interface (see CFStream Socket Additions for information on <br>this). Other functions will not establish the connection.<br><br>
可通过当前网络配置连接到指定的域名或地址,但首先必须建立一个网络连接。连接必须通过CFSocketSteam编程接口建立,其它函数将无法建立此连接。<br><br>
<span style="">kSCNetworkReachabilityFlagsIsLocalAddress</span> :
 
The specified node name or address is one that is associated with a network interface on the current system.<br><br>指定的域名或地址与当前系统的网络接口相关(即本地的网络地址)。<br><br>
<span style="">kSCNetworkReachabilityFlagsIsDirect</span> :
 
Network traffic to the specified node name or address will not go through a gateway, but is routed directly to one of the interface in the system.<br><br>网络流量将不通过网关,而会直接的导向系统中的接口。<br><br>

3. 代码示例:

+ (BOOL) connectedToNetwork
{
    // 创建零地址,0.0.0.0地址表示查询本机的网络连接状态
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;
    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;
    // Get connect flags
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);
    // 如果不能获取连接标志,则不能连接网络,直接返回
    if (!didRetrieveFlags)
    {
         return NO;
    }
 
    // 根据获得的连接标志进行判断
    BOOL isReachable = flag & kSCNetworkFlagsReachable;
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
 
    return (isReachable && !needsConnection) ? YES : NO;
}

//-判断当前网络是否可用

+(BOOL) isNetworkEnabled

{

BOOL bEnabled = FALSE;

NSString *url = @"www.baidu.com";

SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithName(NULL, [url UTF8String]);

SCNetworkReachabilityFlags flags;

bEnabled = SCNetworkReachabilityGetFlags(ref, &flags);

CFRelease(ref);

if (bEnabled) {

//        kSCNetworkReachabilityFlagsReachable:能够连接网络

//        kSCNetworkReachabilityFlagsConnectionRequired:能够连接网络,但是首先得建立连接过程

//        kSCNetworkReachabilityFlagsIsWWAN:判断是否通过蜂窝网覆盖的连接,比如EDGE,GPRS或者目前的3G.主要是区别通过WiFi的连接。

BOOL flagsReachable = ((flags & kSCNetworkFlagsReachable) != 0);

BOOL connectionRequired = ((flags & kSCNetworkFlagsConnectionRequired) != 0);

BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;

bEnabled = ((flagsReachable && !connectionRequired) || nonWiFi) ? YES : NO;

}

return bEnabled;

}

Info.plist中对UIRequiresPersistentWifi, 可让程序持续保持无线网络的连接状态。

IOS(SystemConfiguration)框架中关于测试连接网络状态相关方法的更多相关文章

  1. Spring框架中 配置c3p0连接池 完成对数据库的访问

    开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...

  2. Spring框架中 配置c3p0连接池

    开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...

  3. SSM框架中数据库无法连接的问题

    首先是SSM框架中所有的配置都是没有问题的,而且项目在其他人的环境上也能正常访问数据库:那么最有可能的就是数据库版本的问题导致数据库连接不上,服务器给我的报错是: 15:37:25.902 [C3P0 ...

  4. VM虚拟机中Ubuntu无法连接网络

    之前Ubuntu是可以上网的,但是今天打开后发现上不了网了,经过一番折腾,弄好了,记录下. 方案一:将网络连接设置为自定义NAT VM ->设置-> 硬件->网络适配器 这么已修改就 ...

  5. iOS开发之XCode模拟器不能连接网络

    新装的Xcode7 编译程序 出现 #warning: 获取app配置信息失败: The resource could not be loaded because the App Transport ...

  6. Unit Test测试框架中的测试的执行顺序

    [ClassInitialize()] [ClassCleanup()] [TestInitialize()] [TestMethod] [TestCleanup()] 在执行一个或多个[TestMe ...

  7. 安卓中使用HttpURLConnection连接网络简单示例 --Android网络编程

    MainActivity.java: package thonlon.example.cn.httpurlconnectionpro; import android.os.Bundle;import ...

  8. 用ASP创建API。NET Core (Day2):在ASP中创建API。网络核心

    下载PDF article - 1.5 MB 下载source - 152.4 KB 下载source - 206.3 KB 下载source code from GitHub 表的内容 中间件路线图 ...

  9. 用BroadcastReceiver监听网络状态的变化

    在用户浏览网络信息时,如果网络突然断开,可以及时的提醒用户网络已断开.要实现这个功能,我们可以实现一个广播接收者来接收网络状态改变的广播,当由连接状态变为断开状态时,系统会发送一条广播,广播接收者接收 ...

随机推荐

  1. [转载]Soap安全测试入门

    http://www.cnblogs.com/zerotest/p/4670902.html 在SoapUI4.0引入的安全测试特点使它非常容易为你来验证你的目标服务的功能性安全,就可以评估您的系统常 ...

  2. ARM流水线关键技术分析与代码优化

    引 言    流水线技术通 过多个功能部件并行工作来缩短程序执行时间,提高处理器核的效率和吞吐率,从而成为微处理器设计中最为重要的技术之一.ARM7处理器核使用了典型三级流 水线的冯·诺伊曼结构,AR ...

  3. LayoutInflater和inflate()方法的用法

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 实现LayoutInflater的实例化共有3种方法, (1).通过SystemService获得 Layou ...

  4. spring关于urlpattern

    视图解析器(ViewResolver)注册中央调度器定制处理器jsp页面搭建springmvc.xml配置效果图第一个案例提升----视图解析器关于urlpattern说法最好配成*.do 不能配成/ ...

  5. C#与Java中相等关系

    最近在学习java的语法,发现java中两个字符串某些情况下不能用==这个来判断,比如自己定义的类中有String属性,那么就不能用==判断两个是否相等.这才开始比较C#和Java在这里的区别,最后发 ...

  6. JQuery时间轴timeline插件的学习-Lateral On-Scroll Sliding with jQuery+technotarek / timeliner

    一.Lateral On-Scroll Sliding with jQuery的使用 View demo      Download source 1. HTML结构 <div id=" ...

  7. AngularJS directive入门例子

    这是<AngularJS>这本书里面提供的一个例子: JS代码: var expanderModule=angular.module('expanderModule', []) expan ...

  8. 怎么设置 mysql 多主复制

    更新 其实本文主要来自www.digitalocean.com ,但是我没有买他们家的 VPS 用来 demo 了.只是用vagrant 来模拟了. 介绍 说说关于通过两台 vps 来扩展 mysql ...

  9. SSH由WAS/Tomcat/Weblogic迁移到JBOSS

    又是一个凌晨,又一次搞项目在新的中间件上的可部署性验证... 原来将项目部署到was7上,花了三个晚上到凌晨1点多的时间,总结出了只要将common-logging和wodenxx.jar两个jar包 ...

  10. S1:对象与JSON

    JSON全称为JavaScript对象表示法(JavaScript Object Notation). JSON是JavaScript中对象的字面量,是对象的表示方法,通过使用JSON,可以减少中间变 ...