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 a certain extent: [Request
Settimeoutseconds: 5]; If nsurlrequest is used, you can ignore the local cache and add the timeout method: nsurlrequest * request = [nsurlrequest requestwithurl: url1 cachepolicy: interval timeoutinterval: 10]; after studying this, 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. The mistake is that I used the items used to determine the network connection type to determine the network connectivity. So I used the tools reasonably to do the right thing. :) I changed from my blog in Rainbird.
Use reachability to detect network connection conditions in real time
// Enable the notification at the startup of the program
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
{
//.....
// Enable Network Monitoring
[[Nsicationcenter center defacenter center] addobserver: Self
Selector: @ selector (reachabilitychanged :)
Name: kreachabilitychangednotification
Object: Nil];
Hostreach = [[reachability reachabilitywithhostname: @ "www.google.com"] retain]; // can be initialized in multiple forms
[Hostreach startnotifier]; // starts listening and starts a run loop.
[Self updateinterfacewithreachability: hostreach];
//.....
}
// Connection change
-(Void) reachabilitychanged: (nsnotification *) Note
{
Reachability * curreach = [Note object];
Nsparameterassert ([curreach iskindofclass: [reachability class]);
[Self updateinterfacewithreachability: curreach];
}
// Process the changed connection
-(Void) updateinterfacewithreachability: (reachability *) curreach
{
// The action to respond to connection changes.
Networkstatus status = [curreach currentreachabilitystatus];
If (status = notreachable) {// The status is displayed when the network is not connected.
Uialertview * Alert = [[uialertview alloc] initwithtitle: @ "My app name"
Message: @ "notreachable"
Delegate: Nil
Cancelbuttontitle: @ "yes" otherbuttontitles: Nil];
[Alert show];
[Alert release];
}
}