(2). Drag reachability.h,reachability.m into the project (Library non arc)
(3). Import Systemconfiguration.framework
(4). Usage
(5) If the difference between 2G, 2.5G, 3G network cannot be distinguished, then we can refactor the Networkstatusforflags method in REACHABILITY.M:
-(NetworkStatus) Networkstatusforflags: (scnetworkreachabilityflags) Flags
{
if ((Flags & kscnetworkreachabilityflagsreachable) = = 0)
{
return notreachable;
}
BOOL retVal = notreachable;
if ((Flags & kscnetworkreachabilityflagsconnectionrequired) = = 0)
{
If target host is reachable and no connection are required
Then we ' ll assume (for now) that your on Wi-Fi
RetVal = Reachableviawifi;
}
if (((Flags & Kscnetworkreachabilityflagsconnectionondemand)!= 0) | |
(Flags & Kscnetworkreachabilityflagsconnectionontraffic)!= 0))
{
... and the connection is On-demand (or on-traffic) if the
Calling application is using the Cfsocketstream or higher APIs
if ((Flags & kscnetworkreachabilityflagsinterventionrequired) = = 0)
{
... 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 if 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 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 link";
Break
Case Reachableviawifi:
Connectionkind = @ "The network type currently in use is WiFi";
Break
Case REACHABLEVIA3G:
Connectionkind = @ "The type of network link currently in use is Wwan (3G)";
Break
Case REACHABLEVIA2G:
Connectionkind = @ "The type of network link currently in use is Wwan (2G)";
Break
Default
Break
}
}else {
Connectionkind = @ "no network link";
}
}