iOS CoreTelephony Info

获取iOS设备 运营商信息


#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

#pragma mark - CoreTelephoney NetWork Info

+ (NSDictionary *)networkParams{
    CTTelephonyNetworkInfo *ctNetInfo = [CTTelephonyNetworkInfo new];
    
    ctNetInfo.subscriberCellularProviderDidUpdateNotifier = ^(CTCarrier *carrier){
        NSDictionary *carrierInfo = [self carrierInfoFromCarrier:carrier];
        [self setCommonParamFromDict:carrierInfo];
    };
    
    CTCarrier *carrier = ctNetInfo.subscriberCellularProvider;
    NSDictionary *netParams = [self carrierInfoFromCarrier:carrier];
    return netParams;
}

+ (NSDictionary *)carrierInfoFromCarrier:(CTCarrier *)carrier{
    NSMutableDictionary *netParams = [NSMutableDictionary dictionary];
    
    if (carrier) {
        netParams[@"mmb_ct_brand"] = safeEmpty(carrier.carrierName);
        netParams[@"mmb_ct_mcc"] = safeEmpty(carrier.mobileCountryCode);
        netParams[@"mmb_ct_mnc"] = safeEmpty(carrier.mobileNetworkCode);
        netParams[@"mmb_ct_isoCC"] = safeEmpty(carrier.isoCountryCode);
    }
    return netParams;
}

+ (NSDictionary *)radioAccessTechNeedObserve:(BOOL)needObserveChange{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    CTTelephonyNetworkInfo *ctNetInfo = [CTTelephonyNetworkInfo new];
    if ([ctNetInfo respondsToSelector:@selector(currentRadioAccessTechnology)]) {
        
        if (needObserveChange) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(radioAccessTechChangedNotification) name:CTRadioAccessTechnologyDidChangeNotification object:nil];
        }
        
        NSString *rat = [ctNetInfo currentRadioAccessTechnology];
        if (rat) {
            dict[@"mmb_ct_rat"] = rat;
        }
    }
    return dict;
}

+ (void)radioAccessTechChangedNotification:(NSNotification *)notification{
    NSDictionary *carrierRAT = [self radioAccessTechNeedObserve:NO];
    if ([carrierRAT count] > 0) {
        [self setCommonParamFromDict:carrierRAT];
    }
}