Use Coretelephony in iOS to get the user's current network status (judging 2g,3g,4g)

Source: Internet
Author: User

Objective:

In the project development, often need to use the network. And the user's network environment also needs our developer to pay attention to, according to the different network state to make the corresponding optimization, enhances the user experience.

But usually we only judge whether the user is in the WiFi or mobile data, but in fact, the mobile data is also divided into 2g/3g/4g and other different formats. and different network formats for the user experience

More obvious impact (for projects that depend on the network). Therefore, it is necessary to optimize the different network formats accordingly.

In iOS, whether it is Apple's official reachability class or the more commonly used third-party network class afnetworking, the network environment they provide is limited to

To wifi/data, so we need other ways to get a more detailed network environment for our clients.

The Cttelephonynetworkinfo class is provided in Coretelephony.framework . This class is iOS7 later, before we use this class we need to

Import coretelephony.framework

Then, in the project, import the corresponding. h file

#import <CoreTelephony/CTTelephonyNetworkInfo.h>  // main documents for judging the network format #import  // add get client carrier support

The implementation code is as follows:

Cttelephonynetworkinfo *networkstatus = [[Cttelephonynetworkinfo alloc]init];  // Create a Cttelephonynetworkinfo object NSString *currentstatus  // get current Network description

It can be seen through the. h file of the class. The following description information is available:

/* * Radio Access Technology Values * *Coretelephony_extern NSString * constCtradioaccesstechnologygprs __osx_available_starting (__MAC_NA,__IPHONE_7_0); Coretelephony_extern NSString * constCtradioaccesstechnologyedge __osx_available_starting (__MAC_NA,__IPHONE_7_0); Coretelephony_extern NSString * constCTRADIOACCESSTECHNOLOGYWCDMA __osx_available_starting (__MAC_NA,__IPHONE_7_0); Coretelephony_extern NSString * constCTRADIOACCESSTECHNOLOGYHSDPA __osx_available_starting (__MAC_NA,__IPHONE_7_0); Coretelephony_extern NSString * constCtradioaccesstechnologyhsupa __osx_available_starting (__MAC_NA,__IPHONE_7_0); Coretelephony_extern NSString * Const ctradioaccesstechnologycdma1x __osx_available_starting (__MAC_NA,__IPHONE_ 7_0); Coretelephony_extern NSString * Const CTRadioAccessTechnologyCDMAEVDORev0 __osx_available_starting (__mac_na,__ IPHONE_7_0); Coretelephony_extern NSString * Const Ctradioaccesstechnologycdmaevdoreva __osx_available_starting (__MAC_NA,__ IPHONE_7_0); Coretelephony_extern NSString * Const CTRADIOACCESSTECHNOLOGYCDMAEVDOREVB __osx_available_starting (__MAC_NA,__ IPHONE_7_0); Coretelephony_extern NSString * Const CTRADIOACCESSTECHNOLOGYEHRPD __osx_available_starting (__MAC_NA,__IPHONE_7 _0); Coretelephony_extern NSString * Const Ctradioaccesstechnologylte __osx_available_starting (__MAC_NA,__IPHONE_7_0); 

In fact, these are more familiar with our network format type. After taking these descriptive information, we can use them to make judgments.

if ([Currentstatus isequaltostring:@ "Ctradioaccesstechnologygprs"]) {//GPRS network return; } if ([Currentstatus isequaltostring:@ "Ctradioaccesstechnologyedge"]) {//2.75g's Edge network return; } if ([Currentstatus isequaltostring:@ "CTRADIOACCESSTECHNOLOGYWCDMA"]) {//3g WCDMA network return; } if ([Currentstatus isequaltostring:@ "CTRADIOACCESSTECHNOLOGYHSDPA"] {//3.5g network return,} if ([Currentstatus isequaltostring:@ "Ctradioaccesstechnologyhsupa"]) {//3.5g Network Return,} if ([Currentstatus isequaltostring:@ "ctradioaccesstechnologycdma1x"]) {//cdma2g network return;} if ([ Currentstatus isequaltostring:@ "CTRadioAccessTechnologyCDMAEVDORev0") {//CDMA EVDORev0 (should be counted 3G?) return; if ([Currentstatus isequaltostring:@ "Ctradioaccesstechnologycdmaevdoreva"]) {//CDMA Evdoreva (should also calculate 3G?) return ; } if ([Currentstatus isequaltostring:@ "Ctradioaccesstechnologycdmaevdorevb"]) {//CDMA EVDORev0 (should still be counted 3G?) Return,} if ([Currentstatus isequaltostring:@ "CTRADIOACCESSTECHNOLOGYEHRPD"]) {//HRPD network return;} if ([ Currentstatus isequaltostring:@ "Ctradioaccesstechnologylte"]) {//lte4g network return;}    

Don't look at the code many. In fact, all are the same, just more than the state.

And we take 2g/3g/4g words. Just keep judging.

Name of operator:

Use back the object above
CurrentStatus.subscriberCellularProvider.carrierName

Here also did a demo, based on afnetworking judgment, can judge whether there is no network, WiFi network, mobile data network (mainly described in NetworkStatusMonitor.h)

Call Method:

    [Networkstatusmonitor startwithblock:^(Nsinteger networkstatus) {        //networkstatus is an enumeration value    }];

http://download.csdn.net/detail/nicky2k8/8605057 Demo is here.

Use Coretelephony in iOS to get the user's current network status (judging 2g,3g,4g)

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.