iOS WiFi情報取得、ネットワーク速度テスト
4131 ワード
#import
#import
#import
#import
#import
#include
#include
@interface WifiModel : NSObject
// WiFi , WiFi 、 Mac 、 Data( wifi )
+(NSDictionary *)fetchSSIDInfo;
//
+(NSMutableDictionary *)getDataCounters;
@end
#import "WifiModel.h"
@implementation WifiModel
// WiFi , WiFi 、 Mac 、 Data( 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;
}
//
+(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];
//
NSMutableDictionary *WIFIspeed = [WifiModel getDataCounters];
NSLog(@" wifi = %@ ------------- = %@",WIFImessage,WIFIspeed);
}