IOS determines whether the network and network types are currently connected

Source: Internet
Author: User

Before you have judged whether you are connected to the network, there is no use of reachability in Apple's example.

#import <sys/socket.h>

#import <netinet/in.h>

#import <arpa/inet.h>

#import <SystemConfiguration/SCNetworkReachability.h>

-(BOOL) isconnecttonewtwork

{

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;

}

Now to determine the status of the network, start to consider using reachability,http://www.cnblogs.com/mrhgw/archive/2012/08/01/2617760.html, where there is a download connection, and in HTTP// Bbs.9ria.com/thread-190107-1-1.html refers to refactoring, which makes it possible to determine whether the cellular data uses 3G or 2G.

Networkstatusforflags method Reconstruction in REACHABILITY.M-(NetworkStatus) Networkstatusforflags: (scnetworkreachabilityflags)        flags{if ((Flags & kscnetworkreachabilityflagsreachable) = = 0) {return notreachable;                } BOOL retVal = notreachable; if ((Flags & kscnetworkreachabilityflagsconnectionrequired) = = 0) {//If target host is Reachab  Le and no connection is required//Then we'll assume (for today) 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 &am P kscnetworkreachabilityflagsinterventionrequired) = = 0) {//... and no [user] intervention is needed retVal = Reachableviawifi; }} if ((Flags & kscnetworkreachabilityflagsiswwan) = = Kscnetworkreachabilityflagsiswwan) {if ((Flags & kscnetworkreachabilityflagsreachable) = = kscnetworkreachabilityflagsreachable) {if (Flags & kscnetworkreachabilityflagstransientconnection) = = Kscnetworkreachabilityflagstransientconne                ction) {retVal = REACHABLEVIA3G;                    if ((Flags & kscnetworkreachabilityflagsconnectionrequired) = = kscnetworkreachabilityflagsconnectionrequired) {                RetVal = reachablevia2g; }}}} return retVal;}        Implement the following 2 methods where needed: Check that 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]) {reachability *hostreach = [reachability reachabilitywithhostname:@ "www.google.com"]        ; Switch ([Hostreach currentreachabilitystatus]) {Case notreachable:connectionkind = @ "No networkLink ";            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"; }}

Although such a method can be judged, the results are not accurate.

Find another simple and accurate way to do this:

typedef enum {network_type_none = 0, network_type_2g = 1, network_type_3g = 2, network_type_4g = 3, network_type_5g = 4,//5G is currently guessing the result Network_type_wifi = 5}network_type;-(NSString *) getnetworktype{NSString *connec    Tionkind;        if ([self isconnecttonewtwork]) {uiapplication *app = [UIApplication sharedapplication];        Nsarray *subviews = [[App valueforkey:@ "StatusBar"] valueforkey:@ "Foregroundview"] subviews];        NSNumber *datanetworkitemview = nil; For (ID subview in subviews) {if ([Subview iskindofclass:[nsclassfromstring (@ "Uistatusbardatanetworkitemview")                Class]]) {datanetworkitemview = Subview;            Break        }} network_type NetType = Network_type_none;        NSNumber * num = [Datanetworkitemview valueforkey:@ "Datanetworktype"];               NetType = [num intvalue]; Switch (nettype) {Case network_type_none:connectionkind = @ "NoWeb link ";            Break                Case Network_type_wifi:connectionkind = @ "The network type currently in use is WIFI";            Break                Case Network_type_4g:connectionkind = @ "The type of network link currently in use is Wwan (4G)";            Break                Case Network_type_3g:connectionkind = @ "The type of network link currently in use is Wwan (3G)";            Break                Case Network_type_2g:connectionkind = @ "The type of network link currently in use is Wwan (2G)";            Break        Default:break; }}else {connectionkind = @ "no network link";} return connectionkind;}

IOS determines whether the network and network types are currently connected

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.