- 繼承于NSObject 的.h文件
#import <Foundation/Foundation.h>
#import <SystemConfiguration/CaptiveNetwork.h>
#import <arpa/inet.h>
#import <netinet/in.h>
#import <ifaddrs.h>
#include <net/if.h>
#include <sys/socket.h>
@interface WifiModel : NSObject
//獲取WiFi 信息没隘,返回的字典中包含了WiFi的名稱懂扼、路由器的Mac地址、還有一個Data(轉(zhuǎn)換成字符串打印出來是wifi名稱)
+(NSDictionary *)fetchSSIDInfo;
//網(wǎng)速測試
+(NSMutableDictionary *)getDataCounters;
@end
- 繼承于NSObject 的.m文件
#import "WifiModel.h"
@implementation WifiModel
//獲取WiFi 信息右蒲,返回的字典中包含了WiFi的名稱阀湿、路由器的Mac地址、還有一個Data(轉(zhuǎn)換成字符串打印出來是wifi名稱)
+(NSDictionary *)fetchSSIDInfo {
NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();
if (!ifs) {
return nil;
}
NSDictionary *info = nil;
for (NSString *ifnam in ifs) {
info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
if (info && [info count]) { break; }
}
return info;
}
//網(wǎng)速測試
+(NSMutableDictionary *)getDataCounters
{
BOOL success;
struct ifaddrs *addrs;
const struct ifaddrs *cursor;
const struct if_data *networkStatisc;
int WiFiSent = 0;
int WiFiReceived = 0;
int WWANSent = 0;
int WWANReceived = 0;
NSString *name=[[NSString alloc]init];
NSMutableDictionary *wifiDic = [[NSMutableDictionary alloc]init];
success = getifaddrs(&addrs) == 0;
if (success)
{
cursor = addrs;
while (cursor != NULL)
{
name=[NSString stringWithFormat:@"%s",cursor->ifa_name];
// NSLog(@"ifa_name %s == %@n", cursor->ifa_name,name);
// names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN
if (cursor->ifa_addr->sa_family == AF_LINK)
{
if ([name hasPrefix:@"en"])
{
networkStatisc = (const struct if_data *) cursor->ifa_data;
WiFiSent+=networkStatisc->ifi_obytes;
WiFiReceived+=networkStatisc->ifi_ibytes;
// NSLog(@"WiFiSent %d ==%d",WiFiSent,networkStatisc->ifi_obytes);
//NSLog(@"WiFiReceived %d ==%d",WiFiReceived,networkStatisc->ifi_ibytes);
}
if ([name hasPrefix:@"pdp_ip"])
{
networkStatisc = (const struct if_data *) cursor->ifa_data;
WWANSent+=networkStatisc->ifi_obytes;
WWANReceived+=networkStatisc->ifi_ibytes;
// NSLog(@"WWANSent %d ==%d",WWANSent,networkStatisc->ifi_obytes);
//NSLog(@"WWANReceived %d ==%d",WWANReceived,networkStatisc->ifi_ibytes);
}
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
NSLog(@"nwifiSend:%.2f MBn wifiReceived:%.2f MBn wwansend:%.2f MBn wwanreceived:%.2f MBn",WiFiSent/1024.0/1024.0,WiFiReceived/1024.0/1024.0,WWANSent/1024.0/1024.0,WWANReceived/1024.0/1024.0);
[wifiDic setValue:[NSString stringWithFormat:@"%.f",WiFiSent/1024.0/1024.0] forKey:@"nwifiSend"];
[wifiDic setValue:[NSString stringWithFormat:@"%.f",WiFiReceived/1024.0/1024.0] forKey:@"wifiReceived"];
[wifiDic setValue:[NSString stringWithFormat:@"%.f",WWANSent/1024.0/1024.0] forKey:@"wwansend"];
[wifiDic setValue:[NSString stringWithFormat:@"%.f",WWANReceived/1024.0/1024.0] forKey:@"wwanreceived"];
return wifiDic;
}
@end
3.ViewController
#import "ViewController.h"
#import "WifiModel.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//獲取wifi信息
NSDictionary *WIFImessage = [WifiModel fetchSSIDInfo];
//檢測手機網(wǎng)絡(luò)速度
NSMutableDictionary *WIFIspeed = [WifiModel getDataCounters];
NSLog(@"獲取wifi信息 = %@ ------------- 檢測手機網(wǎng)絡(luò)速度 = %@",WIFImessage,WIFIspeed);
}