21

Im on an application that receive data from server, the problem is when user connect to cellular data (Not 3G or WIFI), it take ages to receive data.

i had implemented this code from this Answer but im not sure if it is effective or not, sometimes it's giving me an accurate type, and sometimes it don't.

here is my code:

- (void)newtworkType {

NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"]subviews];
NSNumber *dataNetworkItemView = nil;

for (id subview in subviews) {
    if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
        dataNetworkItemView = subview;
        break;
    }
}

switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"]integerValue]) {
    case 0:
        NSLog(@"No wifi or cellular");
        break;

    case 1:
        NSLog(@"2G");
        break;

    case 2:
        NSLog(@"3G");
        break;

    case 3:
        NSLog(@"4G");
        break;

    case 4:
        NSLog(@"LTE");
        break;

    case 5:
        NSLog(@"Wifi");
        break;


    default:
        break;
}}

is this the best i can do??, i tried Apple Reachability example, but it can determine if reachabilityForInternetConnection or just reachabilityForLocalWiFi but that not helpfull in my case.

Thanks in advance.

Community
  • 1
  • 1
Mutawe
  • 6,464
  • 3
  • 47
  • 90
  • its working fine but I think its private api so may be apple rejected. Can you please give me suggestion do you upload app on apple store and approved by apple? please comment so I can move forward to upload my app on store. Thanks for help in advance. – Nikunj Jadav Nov 26 '15 at 13:09
  • Crashes on iPhone X. – Illia Vlasov Nov 09 '17 at 14:09

3 Answers3

29

if using iOS 7+ then you can get information from CoreTelephony framework following method :

CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
NSLog(@"Current Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);

Possibles values defined which you will get are as follows : CTRadioAccessTechnologyGPRS, CTRadioAccessTechnologyEdge ,CTRadioAccessTechnologyWCDMA , CTRadioAccessTechnologyLTE etc

Mutawe
  • 6,464
  • 3
  • 47
  • 90
muzz
  • 4,324
  • 2
  • 24
  • 14
  • 3
    this is the actual answer, the rest is hackery, which can stop working any time – Marin Todorov Apr 06 '14 at 07:45
  • Docs say it was available starting in iOS 4: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Reference/CTTelephonyNetworkInfo/Reference/Reference.html – Brian Colavito Jun 13 '14 at 14:16
  • 1
    But `currentRadioAccessTechnology` is available starting in iOS7 . @BrianColavito – Carina Jun 30 '14 at 06:18
  • @muzz currentRadioAccessTechnology returns always CTRadioAccessTechnologyEdge even I change wifi to cellular data. any idea regarding? – Nikunj Jadav Nov 26 '15 at 13:07
16

Make sure that the Status bar is not hidden in your application. if it's not visible it will always return No wifi or cellular because your code reads the text in the Status bar thats all.

this is the best way to solve your problem, just make the Status bar not hidden then the application will get the text about the network type.

M.Alatrash
  • 1,270
  • 1
  • 12
  • 30
  • 4
    i didnt think a moment that if the status bar is set to hidden will cause not reading the value, thank you a million – Mutawe Jun 03 '13 at 14:36
  • @Atrash its working fine but I think its private api so may be apple rejected. Can you please give me suggestion do you upload app on apple store and approved by apple? please comment so I can move forward to upload my app on store. Thanks for help in advance. – Nikunj Jadav Nov 26 '15 at 13:14
0

case 1, with the NSLog(@"2G"); is the case where the phone is on regular cellular data, not 3G, not 4G, and not WiFi.

What you should do is insert code below the NSLog for 2G to prevent the data transfer.

arjunyg
  • 174
  • 1
  • 8
  • Is this the best way i can do it ? – Mutawe May 28 '13 at 06:49
  • As far was I know, yes? As for you requesting more detail (in the bounty), you would need to tell us how to prevent your "data transfer." Since we don't have that code, we can't actually help you. – arjunyg Jun 09 '13 at 20:00