In the above two lectures, we will explain how to get the network status and sock. You may have some knowledge about socket. For more information, see other materials. Next we will introduce
SCNetworkReachability applications. In IOS, The SystemConfiguration framework contains functions related to networking and can check the network connection status. In
SCNetworkReachability. h defines the function for testing the network connection status. For details, refer to the following:
I. Create a test network reference
(1) SCNetworkReachabilityRef SCNetworkReachabilityCreateWithAddress (// create a network connection reference based on the input address
CFAllocatorRef allocator,
// It can be NULL or kCFAllocatorDefault
Const struct sockaddr * address
// The IP address to test the connection
);
Note: When address is 0.0.0.0, You can query the network connection status of the local machine. At the same time, a reference must be released after use.
(2) SCNetworkReachabilityRef SCNetworkReachabilityCreateWithName (// create a network connection reference based on the entered URL
CFAllocatorRef allocator,
// It can be NULL or kCFAllocatorDefault
Const char * nodename
// For example, "www.baidu.com". This parameter is a domain name.
);
Note: When address is 0.0.0.0, You can query the network connection status of the local machine. At the same time, a reference must be released after use.
Release CFRelease (SCNetworkReachabilityRef
Ref );
2. Obtain the network connection status (whether a network connection exists ):
Boolean SCNetworkReachabilityGetFlags (
// Obtain the network connection status
SCNetworkReachabilityRef target,
// Reference of the previously established network connection
SCNetworkReachabilityFlags * flags
// Save the status to determine whether the connection is obtained
);
SCNetworkReachabilityFlags: Save the returned test connection status
Common statuses include:
KSCNetworkReachabilityFlagsReachable: able to connect to the network
Kscnetworkreachabilityflagsconnectionrequired: the network can be connected, but the connection process must be established first.
Kscnetworkreachabilityflagsiswwan: determines whether a connection is covered by a cellular network, such as edge, GPRS, or the current 3G connection.