Direct code bar, IOS7 after the acquisition is more accurate, 7 below I take iphone5 test is unable to distinguish 3g/2g. Even the iphone4 can rise to 7.1.4, and the current mainstream equipment under 7 of the system is very few, although not perfect, but the impact is not too big
#Import<CoreTelephony/CTTelephonyNetworkInfo.h>
String Getnetworktype ()
{
String Strnetworktype ="";
//Create 0 address, 0.0.0.0 address indicates the network connection status of the query native
struct Sockaddr_storage zeroaddress;
Bzero (&zeroaddress, sizeof (zeroaddress));
Zeroaddress.ss_len = sizeof (zeroaddress);
zeroaddress.ss_family = af_inet;
//Recover reachability flags
Scnetworkreachabilityref defaultroutereachability = scnetworkreachabilitycreatewithaddress (NULL, struct sockaddr *) &zeroaddress);
Scnetworkreachabilityflags flags;
//Get the sign of the connection
BOOL didretrieveflags = Scnetworkreachabilitygetflags (defaultroutereachability, &flags);
Cfrelease (defaultroutereachability);
//If you cannot get the connection flag, you cannot connect to the network and return directly
if(!didretrieveflags)
{
returnStrnetworktype;
}
if((Flags & kscnetworkreachabilityflagsconnectionrequired) = = 0)
{
//If target host is reachable and no connection is required
//Then we'll assume (for now) so your on Wi-Fi
Strnetworktype ="WIFI";
}
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
Strnetworktype ="WIFI";
}
}
if((Flags & kscnetworkreachabilityflagsiswwan) = = Kscnetworkreachabilityflagsiswwan)
{
if([[[[Uidevice Currentdevice] systemversion] floatvalue] >= 7.0)
{
Cttelephonynetworkinfo * info = [[Cttelephonynetworkinfo alloc] init];
NSString *currentradioaccesstechnology = info.currentradioaccesstechnology;
if(currentradioaccesstechnology)
{
if([Currentradioaccesstechnology Isequaltostring:ctradioaccesstechnologylte])
{
Strnetworktype ="4G";
}
Elseif([currentradioaccesstechnology Isequaltostring:ctradioaccesstechnologyedge] | | [Currentradioaccesstechnology Isequaltostring:ctradioaccesstechnologygprs])
{
Strnetworktype ="2G";
}
Else
{
Strnetworktype ="3G";
}
}
}
Else
{
if((Flags & kscnetworkreachabilityflagsreachable) = = kscnetworkreachabilityflagsreachable)
{
if((Flags & kscnetworkreachabilityflagstransientconnection) = = kscnetworkreachabilityflagstransientconnection)
{
if((Flags & kscnetworkreachabilityflagsconnectionrequired) = = kscnetworkreachabilityflagsconnectionrequired)
{
Strnetworktype ="2G";
}
Else
{
Strnetworktype ="3G";
}
}
}
}
}
if(Strnetworktype = ="") {
Strnetworktype ="WWAN";
}
NSLog (@ "Getnetworktype () Strnetworktype:%s", Strnetworktype.c_str ());
returnStrnetworktype;
}
Reference Links:
http://blog.csdn.net/justinjing0612/article/details/8741261
http://www.jianshu.com/p/efcfa3c87306
Http://www.cnblogs.com/xiaohuzi1990/p/4341655.html
iOS determines the user's network type (2/3/4G, WiFi)