Determining 3G vs Edge

后端 未结 5 1093
青春惊慌失措
青春惊慌失措 2020-11-27 03:08

I know that the reachability example allows detection of whether network is accessible via Wifi or Cell, but is there a way to determine whether the cell connection is over

相关标签:
5条回答
  • 2020-11-27 03:39

    No, there is no such thing as a public detection of network technology within the cell connectivity.

    0 讨论(0)
  • 2020-11-27 03:44

    As of iOS 7, there's now a public way to do so:

    CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
    NSLog(@"Current Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
    [NSNotificationCenter.defaultCenter addObserverForName:CTRadioAccessTechnologyDidChangeNotification 
                                                    object:nil 
                                                     queue:nil 
                                                usingBlock:^(NSNotification *note) 
    {
        NSLog(@"New Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
    }];
    

    Read up more on my article in objc.io.

    0 讨论(0)
  • 2020-11-27 03:51

    Marginally simplified version of nst's code to silence compiler warnings I got in XCode 4.5:

    - (NSNumber *) dataNetworkTypeFromStatusBar {
    
        UIApplication *app = [UIApplication sharedApplication];
        NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"]    subviews];
        NSNumber *dataNetworkItemView = nil;
    
        for (id subview in subviews) {
            if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
                dataNetworkItemView = subview;
                break;
            }
        }
        return [dataNetworkItemView valueForKey:@"dataNetworkType"];
    }
    

    And the value keys I've found so far:

    • 0 = No wifi or cellular
    • 1 = 2G and earlier? (not confirmed)
    • 2 = 3G? (not yet confirmed)
    • 3 = 4G
    • 4 = LTE
    • 5 = Wifi
    0 讨论(0)
  • 2020-11-27 03:58

    telephonyInfo.currentRadioAccessTechnology Values:

    CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyGPRS          __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
    CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyEdge          __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
    CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyWCDMA         __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
    CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyHSDPA         __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
    CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyHSUPA         __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);
    

    I think that these are all the possible values.

    0 讨论(0)
  • 2020-11-27 03:59

    Using private APIs, you can read this information directly in the status bar.

    https://github.com/nst/MobileSignal/blob/master/Classes/UIApplication+MS.m

    + (NSNumber *)dataNetworkTypeFromStatusBar {
    
        UIApplication *app = [UIApplication sharedApplication];
    
        UIStatusBar *statusBar = [app valueForKey:@"statusBar"];
    
        UIStatusBarForegroundView *foregroundView = [statusBar valueForKey:@"foregroundView"];
    
        NSArray *subviews = [foregroundView subviews];
    
        UIStatusBarDataNetworkItemView *dataNetworkItemView = nil;
    
        for (id subview in subviews) {
            if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
                dataNetworkItemView = subview;
                break;
            }
        }
    
        return [dataNetworkItemView valueForKey:@"dataNetworkType"];
    }
    
    0 讨论(0)
提交回复
热议问题