Skip to content

Instantly share code, notes, and snippets.

@pandaApe
Last active November 28, 2016 03:05
Show Gist options
  • Save pandaApe/f37eef622bcaea41c7b58b5899bfe08d to your computer and use it in GitHub Desktop.
Save pandaApe/f37eef622bcaea41c7b58b5899bfe08d to your computer and use it in GitHub Desktop.
//
// MLDeviceInfoReader.h
//
// Created by PandaApe on 09/11/2016.
// Copyright © 2016 PandaApe. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface MLDeviceInfoReader : NSObject
// 获取设备的信息
- (NSDictionary *)readDeviceInfo;
@end
//
// MLDeviceInfoReader.m
//
// Created by PandaApe on 09/11/2016.
// Copyright © 2016 PandaApe. All rights reserved.
//
#import "MLDeviceInfoReader.h"
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
@implementation MLDeviceInfoReader
- (NSDictionary *)readDeviceInfo{
UIDevice *device = [UIDevice currentDevice];
// 唯一标识
NSString *identifier = device.identifierForVendor.UUIDString;
// 平台
NSString *platform = @"iOS";
// 系统+版本
NSString *system = [NSString stringWithFormat:@"%@ %@",device.systemName,device.systemVersion];
// 设备用户名
NSString *name = device.name;
// 设备型号
NSString *model = device.model;
CGRect rect = [[UIScreen mainScreen] bounds];
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat width = rect.size.width * scale;
CGFloat height = rect.size.height * scale;
// 分辨率
NSString *deviceScale = [NSString stringWithFormat:@"%.1f*%.1f",width,height];
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [info subscriberCellularProvider];
// 运营商名称
NSString *carrierName = [NSString stringWithFormat:@"%@",[carrier carrierName]];
// 网络类型
NSString *networkType = [self getNetWorkStates];
NSDictionary *infoDic = @{
@"X-ML-Identifier":identifier ?:@"",
@"X-ML-Platform":platform ?:@"",
@"X-ML-System":system ?:@"",
@"X-ML-Name":name ?:@"",
@"X-ML-Model":model ?:@"",
@"X-ML-CarrierName":carrierName ?:@"",
@"X-ML-Scale":deviceScale ?:@"",
@"X-ML-NetworkType":networkType ?:@"",
};
return infoDic;
}
// 获取当前的网络类型
- (NSString *)getNetWorkStates{
// 从status bar 取
UIApplication *app = [UIApplication sharedApplication];
NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
NSString *state = [[NSString alloc]init];
int netType = 0;
//获取到网络返回码 ##私有API, 已测试可通过审核##
for (id child in children) {
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
//获取到状态栏
netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
switch (netType) {
case 0:
state = @"无网络";
//无网模式
break;
case 1:
state = @"2G";
break;
case 2:
state = @"3G";
break;
case 3:
state = @"4G";
break;
case 5:
{
state = @"Wi-Fi";
}
break;
default:
break;
}
}
}
//根据状态选择
return state;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment