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)的可連接性, 其包括是否需要一個網絡連接以及在建立網絡連接的過程中是否需要用戶干預。
kSCNetworkReachabilityFlagsIsWWAN : The specified node name or address can be reached via a cellular connection, such ad EDGE or GPRS. 通過EDGE(2G到3G的過渡技術方案,這里可以理解為3G)或者GPRS(2G)連接到指定域名或地址。
kSCNetworkReachabilityFlagsReachable :
The specified node name or address can be reached using the current network configuration.
通過當前的網絡配置可連接到指定的域名和地址。
kSCNetworkReachabilityFlagsTransientConnection :
The specified node name or address can be reached via a transient connection, such as PPP.
通過一個短暫的(網絡)連接可以到達指定的域名或地址,比如PPP(Point to Point Protocol)協議。
kSCNetworkReachabilityFlagsConnectionRequired :
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.
可通過當前的網絡配置連接到指定的域名或地址,但首先必須建立一個網絡連接。如果此標識(kSCNetworkReachabilityFlagsConnectionRequired)被設定,那么標識kSCNetworkReachabilityFlagsConnectionOnTraffic, kSCNetworkReachabilityFlagsConnectionOnDemand或者kSCNetworkabilityFlagsIsWWAN通常應被設定為指定的網絡連接要求類型。如果用戶必須手動生成此連接, 那么kSCNetworkReachabilityFlagsInterventionRequired標識也應要設定。
kSCNetworkReachabilityFlagsConnectionOnTraffic : 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.
可通過當前網絡配置連接到指定的域名或地址,但首先必須建立一個網絡連接,任何到達指定域名或地址的連接都將始於此連接。
kSCNetworkReachabilityFlagsInterventionRequired : The specified node name or address can be reached using the current network configuration, but a connection must first be established.
可通過當前網絡配置連接到指定的域名或地址,但首先必須建立一個網絡連接。
kSCNetworkReachabilityFlagsConnectioniOnDemand : The specified node name or address can be reached using using the current network configuration, but a connection must first be established.
The connection will be established "On Demand" by the CFSocketStream programming interface (see CFStream Socket Additions for information on
this). Other functions will not establish the connection.
可通過當前網絡配置連接到指定的域名或地址,但首先必須建立一個網絡連接。連接必須通過CFSocketSteam編程接口建立,其它函數將無法建立此連接。
kSCNetworkReachabilityFlagsIsLocalAddress : The specified node name or address is one that is associated with a network interface on the current system.
指定的域名或地址與當前系統的網絡接口相關(即本地的網絡地址)。
kSCNetworkReachabilityFlagsIsDirect : 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.
網絡流量將不通過網關,而會直接的導向系統中的接口。
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;
}
Info.plist中對UIRequiresPersistentWifi, 可讓程序持續保持無線網絡的連接狀態。
