1.获取Wifi相关信息
- 一般我们常用Wifi信息有Wifi的名称:SSID,还有Wifi的MAC地址:BSSID
- iOS12后,需要在Capabilities中,激活Access WiFi Information项
- iOS13后,需要申请定位权限
#import <SystemConfiguration/CaptiveNetwork.h>
/** 获取当前Wifi名字 */
- Objc
+ (NSString *)currentWiFiName{
NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
id info = nil;
for (NSString *ifnam in ifs) {
info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
if (info && [info count]) { break; }
}
return info[@"SSID"];
//info[@"BSSID"];
//info[@"SSIDDATA"];
}
此处切记如果不是使用 __bridge_transfer id
转换 记得要手动调用CFRelease
,否则会内存泄漏
- Swift
import SystemConfiguration.CaptiveNetwork
struct WifiTool {
static var currentSSID: String? {
guard let wifiInterfaces = CNCopySupportedInterfaces() else { return nil }
let interfaceArr = CFBridgingRetain(wifiInterfaces) as! Array<String>
if interfaceArr.count > 0 {
let interfaceName = interfaceArr[0] as CFString
let ussafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName)
if (ussafeInterfaceData != nil) {
let interfaceData = ussafeInterfaceData as! Dictionary<String, Any>
return interfaceData["SSID"] as? String
}
}
return nil
};
}
2.Wifi是否就绪
- 我们可能有过这种经验,输完wifi密码后,Wifi名字前还在打圈圈,过完一段时间后状态栏上才有Wifi标识
- 手机连上Wifi后,需要一定时间才能就绪
+ (BOOL)wifiIsReady {
BOOL ret = YES;
struct ifaddrs * first_ifaddr, * current_ifaddr;
NSMutableArray * activeInterfaceNames = [NSMutableArray array];
getifaddrs( &first_ifaddr );
current_ifaddr = first_ifaddr;
while( current_ifaddr != NULL ) {
if( current_ifaddr->ifa_addr->sa_family == 0x02 ) {
[activeInterfaceNames addObject:[NSString stringWithFormat:@"%s", current_ifaddr->ifa_name]];
}
current_ifaddr = current_ifaddr->ifa_next;
}
ret = [activeInterfaceNames containsObject:@"en0"] || [activeInterfaceNames containsObject:@"en1"];
return ret;
}
3.获取当前Wifi的网关地址
#import <arpa/inet.h>
#import <netinet/in.h>
#import <ifaddrs.h>
- (NSString *)getGatewayIpForCurrentWiFi {
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
/*/
int i=255;
while((i--)>0)
//*/
if(temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
// Get NSString from C String //ifa_addr
//ifa->ifa_dstaddr is the broadcast address, which explains the "255's"
// address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)];
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
//routerIP----192.168.0.255 广播地址
NSLog(@"broadcast address--%@",[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)]);
//--192.168.0.100 本机地址
NSLog(@"local device ip--%@",[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]);
//--255.255.255.0 子网掩码地址
NSLog(@"netmask--%@",[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)]);
//--en0 端口地址
NSLog(@"interface--%@",[NSString stringWithUTF8String:temp_addr->ifa_name]);
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
in_addr_t i = inet_addr([address cStringUsingEncoding:NSUTF8StringEncoding]);
in_addr_t* x = &i;
unsigned char *s = getdefaultgateway(x);
NSString *ip=[NSString stringWithFormat:@"%d.%d.%d.%d",s[0],s[1],s[2],s[3]];
free(s);
return ip;
}