Skip to content

Commit

Permalink
增加手机设备相关信息的获取
Browse files Browse the repository at this point in the history
  • Loading branch information
bingxue314159 committed Jan 9, 2018
1 parent b1c97f3 commit 83fd7e8
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 6 deletions.
6 changes: 6 additions & 0 deletions SuperDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@
08C72E011B25321F00967012 /* REMenuExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 08C72E001B25321F00967012 /* REMenuExample.m */; };
08C72E121B2545B400967012 /* REFrostedViewControllerExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 08C72E111B2545B400967012 /* REFrostedViewControllerExample.m */; };
08C72E151B25461A00967012 /* DEMOMenuViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 08C72E141B25461A00967012 /* DEMOMenuViewController.m */; };
08CADE9F200348A3009BEA38 /* TYGSimInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 08CADE9E200348A3009BEA38 /* TYGSimInfo.m */; };
08CB62411B69F11E0033C1DF /* POPDecayAnimationViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 08CB623F1B69F11E0033C1DF /* POPDecayAnimationViewController.m */; };
08CB62421B69F11E0033C1DF /* POPDecayAnimationViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 08CB62401B69F11E0033C1DF /* POPDecayAnimationViewController.xib */; };
08CDF4AD1CB3883D0052B81B /* TYGPopAnimationDemo.m in Sources */ = {isa = PBXBuildFile; fileRef = 08CDF4AC1CB3883D0052B81B /* TYGPopAnimationDemo.m */; };
Expand Down Expand Up @@ -989,6 +990,8 @@
08C72E111B2545B400967012 /* REFrostedViewControllerExample.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = REFrostedViewControllerExample.m; sourceTree = "<group>"; };
08C72E131B25461A00967012 /* DEMOMenuViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DEMOMenuViewController.h; sourceTree = "<group>"; };
08C72E141B25461A00967012 /* DEMOMenuViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DEMOMenuViewController.m; sourceTree = "<group>"; };
08CADE9D200348A3009BEA38 /* TYGSimInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TYGSimInfo.h; sourceTree = "<group>"; };
08CADE9E200348A3009BEA38 /* TYGSimInfo.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TYGSimInfo.m; sourceTree = "<group>"; };
08CB623E1B69F11E0033C1DF /* POPDecayAnimationViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = POPDecayAnimationViewController.h; sourceTree = "<group>"; };
08CB623F1B69F11E0033C1DF /* POPDecayAnimationViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = POPDecayAnimationViewController.m; sourceTree = "<group>"; };
08CB62401B69F11E0033C1DF /* POPDecayAnimationViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = POPDecayAnimationViewController.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1349,6 +1352,8 @@
082D82701FF48DBC005784A5 /* TYGDeviceDataLibrery.m */,
082D82721FF48E4F005784A5 /* TYGDeviceInfo.h */,
082D82731FF48E4F005784A5 /* TYGDeviceInfo.m */,
08CADE9D200348A3009BEA38 /* TYGSimInfo.h */,
08CADE9E200348A3009BEA38 /* TYGSimInfo.m */,
);
path = TYGSystemInfo;
sourceTree = "<group>";
Expand Down Expand Up @@ -3544,6 +3549,7 @@
08A320AE1B82C0DE00943DE9 /* TYGUrlSchemeMapViewController.m in Sources */,
0843F09F1AE502AE00220F15 /* SmileDetectionViewController.m in Sources */,
08E9C8C51B0AD36A00DDF85C /* PopBasicViewController.m in Sources */,
08CADE9F200348A3009BEA38 /* TYGSimInfo.m in Sources */,
08F483ED1B95742600814233 /* IFTTTJazzHandsViewController.m in Sources */,
08F27D561B550ABC0039DF49 /* XIBTestItem.m in Sources */,
08F27D511B550ABC0039DF49 /* StylingViewController.m in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions SuperDemo/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<string>iOS10中,加强了隐私权限,访问相册(不配置,闪退)</string>
<key>NSCalendarsUsageDescription</key>
<string>iOS10中,加强了隐私权限,访问日历(不配置,闪退)</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>iOS10中,Speech framework语音转文字权限(语音识别)</string>
<key>UIAppFonts</key>
<array>
<string>Onboard-sf-outer-limits.upright.ttf</string>
Expand Down
8 changes: 8 additions & 0 deletions SuperDemo/Supporting Files/PrefixHeader.pch
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@
#import <Foundation/Foundation.h>
#endif

//重写NSLog,Debug模式下打印日志和当前行数
//可以用printf来替代NSLog,这样输出的信息中就不会带有时间等信息。
#if DEBUG
#define NSLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(FORMAT, ...) nil
#endif

#endif
6 changes: 4 additions & 2 deletions SuperDemo/Utility/TYG_Utility/TYGSystemInfo/TYGDeviceInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
/** 容量转换 */
-(NSString *)fileSizeToString:(unsigned long long)fileSize;

/** 当前屏幕分辨率 */
- (CGSize)getScreenSize;
/** 获取设备型号 */
- (const NSString *)getDeviceName;
/** 获取设备颜色 */
/** 获取设备颜色(私有API) */
- (NSString *)getDeviceColor;
/** 获取设备外壳颜色 */
/** 获取设备外壳颜色(私有API) */
- (NSString *)getDeviceEnclosureColor;
/** 获取mac地址 */
- (NSString *)getMacAddress;
Expand Down
10 changes: 10 additions & 0 deletions SuperDemo/Utility/TYG_Utility/TYGSystemInfo/TYGDeviceInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ - (NSString *)getMacAddress {
}

#pragma mark - 系统信息
//当前屏幕分辨率
- (CGSize)getScreenSize{
CGRect rect = [[UIScreen mainScreen] bounds];
CGFloat scale = [[UIScreen mainScreen] scale];
CGFloat width = rect.size.width * scale;
CGFloat height = rect.size.height * scale;

return CGSizeMake(width, height);
}

// 获取设备型号
- (const NSString *)getDeviceName {
return [[TYGDeviceDataLibrery sharedLibrery] getDiviceName];
Expand Down
37 changes: 37 additions & 0 deletions SuperDemo/Utility/TYG_Utility/TYGSystemInfo/TYGSimInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// TYGSimInfo.h
// SuperDemo
//
// Created by 谈宇刚 on 2018/1/8.
// Copyright © 2018年 TYG. All rights reserved.
//
// 获取Sim卡信息

#import <Foundation/Foundation.h>

@interface TYGSimInfo : NSObject

+ (instancetype)sharedManager;

/**
获取当前网络的类型(ios7之后可以按照以下方式获取。方便而且类型多)
@return 网络类型(3G,4G……)
*/
- (NSString *)getCurrentRadioAccessTechnology;

/**
获取sim卡信息
@return NSDictionary
*/
- (NSDictionary *)getcarrierInfo;


/**
获取网络环境
*/
+ (NSString *)networktype;


@end
147 changes: 147 additions & 0 deletions SuperDemo/Utility/TYG_Utility/TYGSystemInfo/TYGSimInfo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
//
// TYGSimInfo.m
// SuperDemo
//
// Created by 谈宇刚 on 2018/1/8.
// Copyright © 2018年 TYG. All rights reserved.
//

#import "TYGSimInfo.h"
#import<CoreTelephony/CTTelephonyNetworkInfo.h>
#import<CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>

@interface TYGSimInfo()

@property (nonatomic, strong) CTTelephonyNetworkInfo *telephonyInfo;

@end

@implementation TYGSimInfo

+ (instancetype)sharedManager {
static TYGSimInfo *_manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_manager = [[TYGSimInfo alloc] init];
_manager.telephonyInfo = [[CTTelephonyNetworkInfo alloc] init];
});
return _manager;
}

/**
获取sim卡信息
@return NSDictionary
*/
- (NSDictionary *)getcarrierInfo{

CTCarrier *carrier = [_telephonyInfo subscriberCellularProvider];
NSString *carrierName = [carrier carrierName]; //供应商名称(中国联通 中国移动)
NSString *mobileCountryCode = carrier.mobileCountryCode; //所在国家编号(MCC)
NSString *mobileNetworkCode = carrier.mobileNetworkCode; //供应商网络编号(MNC)
NSString *isoCountryCode = carrier.isoCountryCode;//isoCountryCode
NSString *allowsVOIP = carrier.allowsVOIP ? @"YES" : @"NO"; //是否允许voip

NSLog(@"carrier:%@", [carrier description]);

NSMutableDictionary *carDic = [NSMutableDictionary dictionaryWithCapacity:5];
[carDic setObject:carrierName forKey:@"carrierName"];
[carDic setObject:mobileCountryCode forKey:@"mobileCountryCode"];
[carDic setObject:mobileNetworkCode forKey:@"mobileNetworkCode"];
[carDic setObject:isoCountryCode forKey:@"isoCountryCode"];
[carDic setObject:allowsVOIP forKey:@"allowsVOIP"];

return carDic;
}


/**
获取当前网络的类型(ios7之后可以按照以下方式获取。方便而且类型多)
@return 网络类型(3G,4G……)
*/
- (NSString *)getCurrentRadioAccessTechnology{

if ([CTTelephonyNetworkInfo instancesRespondToSelector:@selector(currentRadioAccessTechnology)]) {
/*
CTRadioAccessTechnologyGPRS //介于2G和3G之间,也叫2.5G ,过度技术
CTRadioAccessTechnologyEdge //EDGE为GPRS到第三代移动通信的过渡,EDGE俗称2.75G
CTRadioAccessTechnologyWCDMA
CTRadioAccessTechnologyHSDPA //亦称为3.5G(3?G)
CTRadioAccessTechnologyHSUPA //3G到4G的过度技术
CTRadioAccessTechnologyCDMA1x //3G
CTRadioAccessTechnologyCDMAEVDORev0 //3G标准
CTRadioAccessTechnologyCDMAEVDORevA
CTRadioAccessTechnologyCDMAEVDORevB
CTRadioAccessTechnologyeHRPD //电信使用的一种3G到4G的演进技术, 3.75G
CTRadioAccessTechnologyLTE //接近4G
*/
return _telephonyInfo.currentRadioAccessTechnology;
}

return NULL;
}

/**
获取网络环境
*/
+ (NSString *)networktype {
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;
}
}

NSString *netname = @"";
switch ([[dataNetworkItemView valueForKey: @ "dataNetworkType"] integerValue]) {
case 0:
netname = @"No wifi or cellular";
break;
case 1:
netname = @"2G";
break;
case 2:
netname = @"3G";
break;
case 3:
netname = @"4G";
break;
case 4:
netname = @"LTE";
break;
case 5:
netname = @"Wifi";
break;
default:
break;
}
return netname;
}

- (void)test{
//1.当sim卡更换时弹出此窗口
//2.当你的 iPhone 漫游到了其他网路的时候,就会执行你这段 block
_telephonyInfo.subscriberCellularProviderDidUpdateNotifier = ^(CTCarrier *carrier) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"carrier:%@", [carrier description]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Sim card changed" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];

[alert show];
});
};

//CTCallCenter 的用途是用来监控是不是有电话打进来、正在接听、或是已经挂断
//而 CTCall 则是将每一则通话事件包装成一个物件
CTCallCenter *center = [[CTCallCenter alloc] init];
center.callEventHandler = ^(CTCall *call) {
NSLog(@"call:%@", [call description]);
};
}

@end
29 changes: 25 additions & 4 deletions SuperDemo/ViewController/four/TYGSystemInfoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "TYGDeviceInfo.h"
#import "CommonHeader.h"
#import "TYGNetworkUtility.h"
#import "TYGSimInfo.h"

@interface TYGSystemInfoViewController (){

Expand Down Expand Up @@ -54,8 +55,7 @@ - (void)didReceiveMemoryWarning {
//初始化数据
- (void)createData {

headerTitles = @[@"基本信息",@"电池",@"CPU",@"内存",@"存储",@"其它",@"网络(en0)"];

headerTitles = @[@"基本信息",@"电池",@"CPU",@"内存",@"存储",@"其它",@"网络(en0)",@"SIM"];

//基本信息
const NSString *deviceName = [[TYGDeviceInfo sharedManager] getDeviceName];//设备名称
Expand Down Expand Up @@ -143,13 +143,14 @@ - (void)createData {
[detailTitlesArray addObject:diskDetailArray];

//其它
NSString *deviceSize = NSStringFromCGSize([[TYGDeviceInfo sharedManager] getScreenSize]);
NSString *DeviceColor = [[TYGDeviceInfo sharedManager] getDeviceColor];
NSString *DeviceEnclosureColor = [[TYGDeviceInfo sharedManager] getDeviceEnclosureColor];
NSString *MacAddress = [[TYGDeviceInfo sharedManager] getMacAddress];
NSString *canMakePhoneCall = [[TYGDeviceInfo sharedManager] canMakePhoneCall] ? @"" : @"";

NSArray *otherTitleArray = @[@"设备颜色(私有API)",@"设备外壳颜色(私有API)",@"mac地址",@"是否可打电话"];
NSArray *otherDetailArray = @[DeviceColor,DeviceEnclosureColor,MacAddress,canMakePhoneCall];
NSArray *otherTitleArray = @[@"分辨率",@"设备颜色(私有API)",@"设备外壳颜色(私有API)",@"mac地址",@"是否可打电话"];
NSArray *otherDetailArray = @[deviceSize,DeviceColor,DeviceEnclosureColor,MacAddress,canMakePhoneCall];

[titlesArray addObject:otherTitleArray];
[detailTitlesArray addObject:otherDetailArray];
Expand Down Expand Up @@ -178,6 +179,26 @@ - (void)createData {

[titlesArray addObject:netTitleArray];
[detailTitlesArray addObject:netDetailArray];

//SIM
NSMutableArray *simTitleArray = [NSMutableArray arrayWithObjects:@"网络类型",@"网络环境",@"手机号码(私有API)",@"手机号码", nil];
NSMutableArray *simDetailArray = [NSMutableArray arrayWithCapacity:10];

NSString *networktype = [[TYGSimInfo sharedManager] getCurrentRadioAccessTechnology];//获取当前网络的类型
NSString *networktype2 = [TYGSimInfo networktype];//获取网络环境
[simDetailArray addObject:SAFE_STRING(networktype)];
[simDetailArray addObject:SAFE_STRING(networktype2)];

NSDictionary *carrierInfo = [[TYGSimInfo sharedManager] getcarrierInfo];//获取sim卡信息
for (NSString *key in [carrierInfo allKeys]) {
NSString *value = [carrierInfo objectForKey:key];
[simTitleArray addObject:key];
[simDetailArray addObject:value];
}

[titlesArray addObject:simTitleArray];
[detailTitlesArray addObject:simDetailArray];

}

#pragma mark - Table view data source
Expand Down

0 comments on commit 83fd7e8

Please sign in to comment.