Reachability Check Network Status

Source: Internet
Author: User

Reachability is a database officially provided by Apple to check the network status. It must be used by every network-based application to check the network status. Of course, I am no exception. however, just as I was full of confidence and used this database, I suddenly found that the network-based program I wrote was not so smooth, especially after careful check, it is determined that it is unreasonable to use the Reachability. The gap between the two moments makes the previous minute very attractive. "writing a program is an art, "How can I use a program to enjoy it?

In this case, the Wi-Fi connection is established, but the Internet access is not allowed. in this case, the Reachability check considers the device to be in wifi (yes, it seems to be in wifi ), if, at this time, you certainly think that you can access the Internet normally to obtain network data, you can wait for the network request to time out. The default timeout time seems to be 15 seconds, the program has been stuck for so long and is unacceptable for the user experience. in the past, I always thought Reachability was amazing. I could check whether it was a Wi-Fi network or a 3G network, and secretly wondered which server it was connected to determine the network conditions, after reading this carefully, I think it is quite a failure. When it is used to distinguish between network connections, it simply checks whether there are any IP addresses. it is no wonder that the Wi-Fi network is unavailable. what is the solution? If you use ASI for http requests, you can simply set the timeout time to avoid this problem to some extent: [request setTimeOutSeconds: 5]; if you use NSURLRequest, you can use the ignore local cache and add timeout method: NSURLRequest * request = [NSURLRequest requestWithURL: url1 cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval: 10, I suddenly found a way to judge the network connection:

 


NSURL * url1 = [NSURL URLWithString: @ "http://blog.cnrainbird.com"];
NSURLRequest * request = [NSURLRequest requestWithURL: url1 cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval: 10];
NSHTTPURLResponse * response;
[NSURLConnection sendSynchronousRequest: request returningResponse: & response error: nil];
If (response = nil ){
NSLog (@ "No network ");
}
Else {
NSLog (@ "network connection ");
}


Simply put, you need to request a network request and set the timeout time, and then determine the network connection based on the returned results. of course, your network request should preferably return very simple data, and the request address should also be the network address you want to request (you think about: What You Want To request for baidu, is google's network connectivity useful ?). The preceding code is stuck in the main thread.
I think there is nothing wrong with Reachability. If it is wrong, I am wrong. I used the items used to determine the network connection type to determine the network connectivity. So we can use the tools reasonably to do the right thing :)

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.