获取wifi信息兼容iOS 14以上设备

This commit is contained in:
kai60
2022-04-29 18:07:47 +08:00
parent 64d5967585
commit 27b8ee3653
16 changed files with 474 additions and 485 deletions
+4 -2
View File
@@ -9,6 +9,9 @@
#import <Foundation/Foundation.h>
#import "UIColor+HexString.h"
#import <SystemConfiguration/CaptiveNetwork.h>
#import <NetworkExtension/NetworkExtension.h>
#import "Reachability.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
@interface CommonUtils : NSObject
typedef enum{
@@ -22,8 +25,6 @@ typedef enum{
//系统默认整体页面背景色
#define COLOR_Background HEXCOLOR(@"#f6f6f6")
/// 获取WiFi信息
+ (NSDictionary *)getSSIDInfo;
/**
获取手机型号
*/
@@ -161,4 +162,5 @@ typedef enum{
CGAffineTransform GetCGAffineTransformRotateAroundPoint(float centerX, float centerY ,float x ,float y ,float angle);
+ (void)interfaceOrientation:(UIInterfaceOrientation)orientation;
+ (UIBarButtonItem*)ItemWithTitle:(NSString *)title target:(id)target action:(SEL)action titleColor:(UIColor*)titleColor;
+ (void )getNetworkTypeComplete:(void(^)(NSString*netconnType,NSString *BSSID,NSString *SSID))complete;
@end
+94 -14
View File
@@ -312,20 +312,7 @@
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
+ (NSDictionary *)getSSIDInfo
{
NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
NSDictionary *SSIDInfo;
for (NSString *interfaceName in interfaceNames) {
SSIDInfo = CFBridgingRelease(
CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
BOOL isNotEmpty = (SSIDInfo.count > 0);
if (isNotEmpty) {
break;
}
}
return SSIDInfo;
}
//view围绕任意一点旋转
CGAffineTransform GetCGAffineTransformRotateAroundPoint(float centerX, float centerY ,float x ,float y ,float angle)
{
@@ -745,4 +732,97 @@ if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
}
return string;
}
+ (void )getNetworkTypeComplete:(void(^)(NSString*netconnType,NSString *BSSID,NSString *SSID))complete
{
NSString *netconnType = @"";
NSString *BSSID = @"";
NSString *SSID = @"";
switch ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus])
{
case NotReachable:// 没有网络
{
netconnType = @"no network";
}
break;
case ReachableViaWiFi:// Wifi
{
netconnType = @"Wifi";
if (@available(iOS 14.0, *))
{
[NEHotspotNetwork fetchCurrentWithCompletionHandler:^(NEHotspotNetwork * _Nullable currentNetwork) {
if (complete) {
complete(netconnType,[CommonUtils standardFormateMAC:[CommonUtils getNotNilStr:currentNetwork.BSSID]],[CommonUtils getNotNilStr:currentNetwork.SSID]);
}
}];
return;
}
else
{
NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();
NSDictionary *info = nil;
for (NSString *ifnam in ifs)
{
info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
if (info && [info count]) break;
}
NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
for (NSString *key in info.allKeys)
{
NSObject *value = [info objectForKey:key];
if ([value isKindOfClass:NSString.class]) {
[resultDic setObject:value forKey:key];
}
}
BSSID=[CommonUtils getNotNilStr:[CommonUtils standardFormateMAC:resultDic[@"BSSID"]]];
SSID=[CommonUtils getNotNilStr:resultDic[@"SSID"]];
}
}
break;
case ReachableViaWWAN:// 移动数据
{ // 获取手机网络类型
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
NSString *currentStatus = info.currentRadioAccessTechnology;
if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyGPRS"]) {
netconnType = @"GPRS";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMA1x"]){
netconnType = @"2G";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORev0"] || [currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevA"] || [currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevB"] || [currentStatus isEqualToString:@"CTRadioAccessTechnologyWCDMA"]){
netconnType = @"3G";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyLTE"]){
netconnType = @"4G";
}else{
netconnType = @"other";
}
}
break;
default:
break;
}
if (complete) {
complete(netconnType,BSSID,SSID);
}
}
+(NSString *)standardFormateMAC:(NSString *)MAC {
NSArray * subStr = [MAC componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@":-"]];
NSMutableArray * subStr_M = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString * str in subStr) {
if (1 == str.length) {
NSString * tmpStr = [NSString stringWithFormat:@"0%@", str];
[subStr_M addObject:tmpStr];
} else {
[subStr_M addObject:str];
}
}
NSString * formateMAC = [subStr_M componentsJoinedByString:@":"];
return [formateMAC uppercaseString];
}
@end