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, 可讓程序持續保持無線網絡的連接狀態。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM