? Reachability can only distinguish between 2G and 3G networks without network connection, WiFi and wwan (2G & 2.5G & 3g). You only need to reconstruct the networkstatusforflags method to distinguish 2G networks from 3G networks in detail?
?
1. [Code] [C/C ++] Code
// Reachability. m networkstatusforflags method Reconstruction
-(Networkstatus) networkstatusforflags: (scnetworkreachabilityflags) flags
{
If (flags & kscnetworkreachabilityflagsreachable) = 0)
{
Return notreachable;
}
Bool retval = notreachable;
If (flags & kscnetworkreachabilityflagsconnectionrequired) = 0)
Http://www.huiyi8.com/jiaoben?
// If target host is reachable and no connection is required
// Then we'll assume (for now) that your on Wi-Fi
Retval = reachableviawifi;
}
If (flags & kscnetworkreachabilityflagsconnectiondemand )! = 0) |
(Flags & kscnetworkreachabilityflagsconnectiontraffic )! = 0 ))
{
//... And the connection is on-demand (or on-traffic) if
// Calling application is using the cfsocketstream or higher APIs
If (flags & kscnetworkreachabilityflagsinterventionrequired) = 0)
{Jquery Special Effects
//... And no [user] intervention is needed
Retval = reachableviawifi;
}
}
If (flags & kscnetworkreachabilityflagsiswwan) = kscnetworkreachabilityflagsiswwan)
{
If (flags & kscnetworkreachabilityflagsreachable) = kscnetworkreachabilityflagsreachable ){
If (flags & kscnetworkreachabilityflagstransientconnection) = kscnetworkreachabilityflagstransientconnection ){
Retval = reachablevia3g;
If (flags & kscnetworkreachabilityflagsconnectionrequired) = kscnetworkreachabilityflagsconnectionrequired ){
Retval = reachablevia2g;
}
}
}
}
Return retval;
}
// Check whether the current network connection is normal
-(Bool) connectedtonetwork
{
Struct sockaddr_in zeroaddress;
Bzero (& zeroaddress, sizeof (zeroaddress ));
Zeroaddress. sin_len = sizeof (zeroaddress );
Zeroaddress. sin_family = af_inet;
Scnetworkreachabilityref defaultroutereachability = scnetworkreachabilitycreatewithaddress (null, (struct sockaddr *) & zeroaddress );
Scnetworkreachabilityflags flags;
Bool didretrieveflags = scnetworkreachabilitygetflags (defaultroutereachability, & flags );
Cfrelease (defaultroutereachability );
If (! Didretrieveflags ){
Printf ("error. Count not recover network reachability flags \ n ");
Return no;
}
Bool isreachable = flags & kscnetworkflagsreachable;
Bool needsconnection = flags & kscnetworkflagsconnectionrequired;
Return (isreachable &&! Needsconnection )? Yes: No;
}
// Check the network connection type
-(Void) checknetworktype :( ID) sender {
Nsstring * connectionkind;
If ([self connectedtonetwork]) {
Hostreach = [reachability reachabilitywithhostname: @ "www.google.com"];
Switch ([hostreach currentreachabilitystatus]) {
Case notreachable:
Connectionkind = @ "No network connection ";
Break;
Case reachableviawifi:
Connectionkind = @ "the current network type is WiFi ";
Break;
Case reachablevia3g:
Connectionkind = @ "the current network connection type is wwan (3G )";
Break;
Case reachablevia2g:
Connectionkind = @ "the current network connection type is wwan (2g )";
Break;
Default:
Break;
}
} Else {
Connectionkind = @ "No network connection ";
}
}