变更记录
序号 | 录入时间 | 录入人 | 备注 |
---|---|---|---|
1 | 2016-08-18 | Alfred Jiang | - |
方案名称
网络 - 判断连接类型
关键字
网络 \ CTTelephonyNetworkInfo \ 移动网络类型 \ 运营商信息
需求场景
- 需要确认手机移动网络状态和运营商名称等信息
参考链接
- Stack Overflow - CTTelephonyNetworkInfo detect data type(推荐)
- Stack Overflow - Detect carrier connection type (3G / EDGE / GPRS)
详细内容
方案一
CTTelephonyNetworkInfo+CellularConnectionName.h
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <Foundation/Foundation.h>
@interface CTTelephonyNetworkInfo (CellularConnectionName)
- (NSString *)cellularConnectionVersionName;
- (NSString *)cellularConnectionNiceName;
@end
CTTelephonyNetworkInfo+CellularConnectionName.m
#import "CTTelephonyNetworkInfo+CellularConnectionName.h"
@implementation CTTelephonyNetworkInfo (CellularConnectionName)
- (NSString *)cellularConnectionVersionName {
if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]
||[self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
return @"2G";
}
else if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]
||[self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]
||[self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]
||[self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]
||[self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]
||[self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]
||[self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]
||[self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
return @"3G";
}
else if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
return @"4G";
}
return @"UNKNOWN";
}
- (NSString *)cellularConnectionNiceName {
if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
return @"GPRS";
} else if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
return @"EDGE";
} else if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) {
return @"WCDMA";
} else if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) {
return @"HSDPA";
} else if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) {
return @"HSUPA";
} else if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
return @"CDMA1X";
} else if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
return @"CDMAEVDOREV0";
} else if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
return @"CDMAEVDOREVA";
} else if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
return @"CDMAEVDOREVB";
} else if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
return @"EHRPD";
} else if ([self.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
return @"LTE";
}
return @"UNKNOWN";
}
@end
使用
CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
NSLog(@"Current Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
[NSNotificationCenter.defaultCenter addObserverForName:CTRadioAccessTechnologyDidChangeNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note)
{
NSLog(@"New cellularConnectionVersionName: %@", telephonyInfo.cellularConnectionVersionName);
NSLog(@"New cellularConnectionNiceName: %@", telephonyInfo.cellularConnectionNiceName);
}];
方案二(推荐)
CheckNetwork.h
//
// CheckNetwork.h
// CheckNetworkType
//
// Created by viktyz on 16/10/27.
// Copyright © 2016年 AlfredJiang. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CheckNetwork : NSObject
+ (NSString *)networkType;
@end
CheckNetwork.m
//
// CheckNetwork.m
// CheckNetworkType
//
// Created by viktyz on 16/10/27.
// Copyright © 2016年 AlfredJiang. All rights reserved.
//
#import "CheckNetwork.h"
#import <netinet/in.h>
#import <SystemConfiguration/SCNetworkReachability.h>
@implementation CheckNetwork
{
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
{
return @"Not Reachable";
}
if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
(flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
{
if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
{
return @"wifi";
}
}
if((flags & kSCNetworkReachabilityFlagsReachable) == kSCNetworkReachabilityFlagsReachable){
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN){
if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection){
if((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired){
return @"2g";
}
return @"3g";
}
return @"4g";
}
}
if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0){
return @"wifi";
}
return @"Unknown";
}
@end
使用
[CheckNetwork networkType];
效果图
(无)