IPhone determines the current network connection type

Source: Internet
Author: User

? 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 ";
}
}

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.