iOS platform network type detection

Source: Internet
Author: User
Tags bssid

Eldest brother added a demand, connect the server to distinguish between internal and external network, demand understanding, the reality is very skinny ah, no way, dry.

#import <foundation/foundation.h>/* relies on the following framework systemconfiguration Coretelephony */typedef Enum _ networktype{    knetworkoff = 0,    knetworkwifi,    knetworkwlan,//If you cannot get to more detail, the value will be returned uniformly (iOS7 above to obtain)    KNETWORKWLAN2G,    knetworkwlan3g,    Knetworkgprs,    Knetworkedge,    KNETWORKWCDMA,    KNETWORKHSDPA,    Knetworkhsupa,    knetworkcdma1x,    kNetworkCDMAEVDORev0,    Knetworkcdmaevdoreva,    Knetworkcdmaevdorevb,    KNETWORKHRPD,    knetworklte,}networktype; @interface hsnetworkdetect:nsobject// Universal Interface/* Returns the network type enumeration value of 0~2 Networktype */+ (networktype) getnetworktype;/* iOS7 and above, in case of a cellular network, returns the >2 network type enumeration value; otherwise, 2 */+ is still returned ( Networktype) getcellulardatanetworktype;/* detect if the network is available, use reachability */+ (BOOL) isreachable: (nsstring*) address port: ( Nsinteger) port;/* Gets the SSID */+ (nsstring*) Getwifissid of the currently connected WiFi hotspot; @end

#import "HSNetworkDetect.h" #import "Reachability.h" #import <CoreTelephony/CTTelephonyNetworkInfo.h> #import <SystemConfiguration/CaptiveNetwork.h> #define ISIOS7! ([[[[Uidevice Currentdevice] systemversion] floatvalue] <=6.9f) #define SENTENCED_EMPTY (String) (String = (String = Nil)? @ "": string) @implementation Hsnetworkdetect#pragma Mark Universal Interface + (Networktype) getnetworktype{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);    To create a reference to a test connection: scnetworkreachabilityflags flags;    Scnetworkreachabilitygetflags (defaultroutereachability, &flags);    if ((Flags & kscnetworkreachabilityflagsreachable) = = 0) {return knetworkoff;    } networktype retVal = Knetworkoff; if (Flags & KSCNETWORKREACHABILITYFlagsconnectionrequired) = = 0) {retVal = Knetworkwifi;         } if (((Flags & kscnetworkreachabilityflagsconnectionondemand)! = 0) | | (Flags & kscnetworkreachabilityflagsconnectionontraffic)! = 0)) {if (Flags & KSCNETWORKREACHABILITYFLA        gsinterventionrequired) = = 0) {retVal = Knetworkwifi; }} if ((Flags & kscnetworkreachabilityflagsiswwan) = = Kscnetworkreachabilityflagsiswwan) {if (fl AGS & kscnetworkreachabilityflagsreachable) = = kscnetworkreachabilityflagsreachable) {if (Flags & KSC                networkreachabilityflagstransientconnection) = = kscnetworkreachabilityflagstransientconnection) {                RetVal = knetworkwlan3g;                if ((Flags & kscnetworkreachabilityflagsconnectionrequired) = = kscnetworkreachabilityflagsconnectionrequired)                {retVal = knetworkwlan2g; }}}} return Retval;} + (Networktype) getcellulardatanetworktype{if (ISIOS7) {cttelephonynetworkinfo *telephonyinfo = [Cttelephonyn        Etworkinfo new];        nsstring* wlannetwork = telephonyinfo.currentradioaccesstechnology;        if (wlannetwork = = nil) return knetworkoff;        if ([Wlannetwork Isequaltostring:ctradioaccesstechnologygprs]) {return KNETWORKGPRS; } else if ([Wlannetwork Isequaltostring:ctradioaccesstechnologyedge]) {return KNETWORKEDG        E } else if ([Wlannetwork ISEQUALTOSTRING:CTRADIOACCESSTECHNOLOGYWCDMA]) {return KNETWORKWCD        MA; } else if ([Wlannetwork ISEQUALTOSTRING:CTRADIOACCESSTECHNOLOGYHSDPA]) {return KNETWORKHSD        PA; } else if ([Wlannetwork Isequaltostring:ctradioaccesstechnologyhsupa]) {return KNETWORKHSU        PA; } else if ([Wlannetwork ISEQUALTOSTRING:CTRADIOACCESSTECHNOLOGYCDma1x]) {return knetworkcdma1x; } else if ([Wlannetwork isequaltostring:ctradioaccesstechnologycdmaevdorev0]) {return KNETWORKCDM        AEVDORev0; } else if ([Wlannetwork Isequaltostring:ctradioaccesstechnologycdmaevdoreva]) {return KNETWORKCDM        Aevdoreva; } else if ([Wlannetwork Isequaltostring:ctradioaccesstechnologycdmaevdorevb]) {return KNETWORKCDM        Aevdorevb; } else if ([Wlannetwork ISEQUALTOSTRING:CTRADIOACCESSTECHNOLOGYEHRPD]) {return KNETWORKHRP        D } else if ([Wlannetwork Isequaltostring:ctradioaccesstechnologylte]) {return knetworklte        ; }} return Knetworkwlan;}    + (nsstring*) getwifissid{nsarray *ifs = (__bridge ID) cncopysupportedinterfaces ();    ID info = nil; For (NSString *ifnam in ifs) {info = (__bridge ID) cncopycurrentnetworkinfo ((__bridge cfstringref) Ifnam);       if (info && [info count]) {nsstring *ssid=[info objectforkey: (__bridge nsstring *) kcnnet            WORKINFOKEYSSID];            NSString *bssid=[info Objectforkey: (__bridge NSString *) Kcnnetworkinfokeybssid];            NSLog (@ "interfacename:%@ ssid:%@ bssid:%@", ifnam,ssid,bssid);        return SSID; }} return nil;} + (BOOL) isreachable: (nsstring*) address port: (nsinteger) port{reachability* Poreach = [reachability    Reachabilitywithhostname:[nsstring stringwithformat:@ "%@:%d", Address,port]];    BOOL BRet = [Poreach isreachable]; return bRet;} @end

PS---> Boss temper you don't guess, guess guess you don't understand.

iOS platform network type detection

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.