In IOS development, we often use networks. networks are indispensable. When there are network connections and no network connections, different processing and resumable data transfer are inseparable from network detection. Today, I made a simple demo and used a function to encapsulate network detection;
-(BOOL) netWorkIsExistence {
Struct sockaddr_in initAddress; // sockaddr_in is an equivalent data structure to sockaddr.
Bzero (& initAddress, sizeof (initAddress ));
InitAddress. sin_len = sizeof (initAddress );
InitAddress. sin_family = AF_INET; // sin_family is the address family, which is generally in the form of "AF_xxx. Generally, most of them are AF_INET, which represents the TCP/IP protocol family.
SCNetworkReachabilityRef readRouteReachability = SCNetworkReachabilityCreateWithAddress (NULL, (struct sockaddr *) & initAddress); // create a reference for the test connection:
SCNetworkReachabilityFlags flags;
BOOL getRetrieveFlags = SCNetworkReachabilityGetFlags (readRouteReachability, & flags );
CFRelease (readRouteReachability );
If (! GetRetrieveFlags ){
Return NO;
}
BOOL flagsReachable = (flags & kSCNetworkFlagsReachable )! = 0 );
BOOL connectionRequired = (flags & kSCNetworkFlagsConnectionRequired )! = 0 );
Return (flagsReachable &&! ConnectionRequired )? YES: NO;
}
Whether the above content is incomprehensible. If you have never touched socket programming and SCNetworkReachability, please refer to the second lecture.