iOS determines the user's network type (2/3/4G, WiFi)

Source: Internet
Author: User

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)

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.