文章目录
  1. 1. WIFI
  2. 2. 设备基本信息
  3. 3. 电池信息
  4. 4. 手机存储
  5. 5. 内存信息
  6. 6. 获取SIM运营商
  7. 7. 手机信号强度

[TOC]

目前感觉最可靠

WIFI

#####获取WIFI SSID

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#import <SystemConfiguration/CaptiveNetwork.h>

+(NSString *) currentWifiSSID
{
#if TARGET_OS_SIMULATOR
return @"(simulator)";
#else
NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();

id info = nil;
for (NSString *ifnam in ifs) {
info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
if (info && [info count]) {
break;
}
}
NSDictionary *dctySSID = (NSDictionary *)info;
NSString *ssid = [dctySSID objectForKey:@"SSID"] ;

return ssid;
#endif
}

#####wifi IP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include <ifaddrs.h>
#include <net/if.h>



+(NSString *) localWiFiIPAddress
{
BOOL success;
struct ifaddrs * addrs;
const struct ifaddrs * cursor;

success = getifaddrs(&addrs) == 0;
if (success) {
cursor = addrs;
while (cursor != NULL) {
// the second test keeps from picking up the loopback address
if (cursor->ifa_addr->sa_family == AF_INET && (cursor->ifa_flags & IFF_LOOPBACK) == 0)
{
NSString *name = [NSString stringWithUTF8String:cursor->ifa_name];
if ([name isEqualToString:@"en0"]) // Wi-Fi adapter
return [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)];
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
return nil;
}

#####获取BSSID SSID SSIDDATA

1
2
3
4
5
6
7
8
9
10
11
12
- (NSDictionary *)getWIFIDic
{
CFArrayRef myArray = CNCopySupportedInterfaces();
if (myArray != nil) {
CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
if (myDict != nil) {
NSDictionary *dic = (NSDictionary*)CFBridgingRelease(myDict);
return dic;
}
}
return nil;
}

#####WIFI 信号强度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//遍历UIStatusBar获取。
- (void)getSignalStrength{
UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
NSString *dataNetworkItemView = nil;

for (id subview in subviews) {
if([subview isKindOfClass: [NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
dataNetworkItemView = subview;
break;
}
}

int signalStrength = [[dataNetworkItemView valueForKey:@"_wifiStrengthBars"] intValue];

NSLog(@"WIFI信号强度: signal %d", signalStrength);
}

设备基本信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 void getAllinfo(){

UIDevice *device = [[UIDevice alloc] init];
NSString *name = device.name; //获取设备所有者的名称
NSString *model = device.name; //获取设备的类别
NSString *type = device.localizedModel; //获取本地化版本
NSString *systemName = device.systemName; //获取当前运行的系统
NSString *systemVersion = device.systemVersion;//获取当前系统的版本
UIUserInterfaceIdiom idiom = device.userInterfaceIdiom; //0--iphone 1---ipad 不同的屏幕不同的布局
NSArray * a= @[name,model,type,systemName,systemVersion,@(idiom)];
for (id m in a) {
NSLog(@"设备信息 : %@ \n",m);
}
}

电池信息

1
2
3
4
5
6
7
8

-(void) getBatteryInfo
{
NSString *state = getBatteryState();
float level = getBatteryLevel()*100.0;
NSLog(@"电池状态:%@\n 电池电量: %.1f\%%",state,level);
//yourControlFunc(state, level); //写自己要实现的获取电量信息后怎么处理
}

手机存储

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <sys/param.h>
#include <sys/mount.h>

//剩余空间
+ (NSString *) freeDiskSpaceInBytes{
struct statfs buf;
long long freespace = -1;
if(statfs("/var", &buf) >= 0){
freespace = (long long)(buf.f_bsize * buf.f_bfree);
}
return [NSString stringWithFormat:@"手机剩余存储空间为:%.2f G" ,freespace*1.0/(1000*1000*1000)];
}
//总容量

-(long long)getTotalDiskSize
{
struct statfs buf;
unsigned long long freeSpace = -1;
if (statfs("/var", &buf) >= 0)
{
freeSpace = (unsigned long long)(buf.f_bsize * buf.f_blocks);
}

NSLog(@"%.1fG",freeSpace/(1.0*1000*1000*1000));
return freeSpace;
}

内存信息

#####内存大小

1
2
3
4
5
6
7
-(long long)getTotalMemorySize
{
long long m = [NSProcessInfo processInfo].physicalMemory;

NSLog(@"内存信息 %.1f G",m*1.0/(1000*1000*1000));
return m;
}

获取SIM运营商

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#import <CoreTelephony/CTCarrier.h>


NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];

for (id info in infoArray)
{
if ([info isKindOfClass:NSClassFromString(@"UIStatusBarServiceItemView")])
{
NSString *serviceString = [info valueForKeyPath:@"serviceString"];
NSString *loopString = [info valueForKeyPath:@"letterSpacing"];
NSLog(@"公司为:%@ --%@",serviceString,loopString);
return serviceString;
}
}

手机信号强度

1
2
3
4
5
6
7
8
9
10
11
   if ([child isKindOfClass:NSClassFromString(@"UIStatusBarSignalStrengthItemView")]) {
unsigned int outCount =0;
Ivar *ivars = class_copyIvarList([child class], &outCount);
for (int i =0; i < outCount; i++) {
Ivar ivar = ivars[i];
printf("HHHHHH %s\n",ivar_getName(ivar));
}
int signalStrength = [[child valueForKeyPath:@"signalStrengthBars"]intValue];
NSLog(@"电话信号强度 : %d", signalStrength);

}
文章目录
  1. 1. WIFI
  2. 2. 设备基本信息
  3. 3. 电池信息
  4. 4. 手机存储
  5. 5. 内存信息
  6. 6. 获取SIM运营商
  7. 7. 手机信号强度