View the availability of network links in iOS development-lecture 1

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.