In the previous two articles, I introduced how to obtain the network status and sock. At this point, I have a little understanding about socket. Next I 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 // ip address of the connection to be tested
);
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 (// used to 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.