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

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:判断是否通过蜂窝网覆盖的连接,比如EDGEGPRS或者目前的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, 可让程序持续保持无线网络的连接状态。

原文地址:https://www.cnblogs.com/worldtraveler/p/4708092.html