- (id)fetchSSIDInfo
{
NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();
NSLog(@"%s: Supported interfaces: %@", __func__, ifs);
id info = nil;
for (NSString *ifnam in ifs) {
info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
if (info && [info count]) {
break;
}
}
return info ;
}
用socket添加 两个文件 AsyncUdpSocket.h 和 AsyncUdpSocket.m
SystemControm.framework CFNetwork.framework
#import "ViewController.h"
#import <SystemConfiguration/CaptiveNetwork.h>
@interfaceViewController ()
@property(nonatomic,strong) AsyncUdpSocket *socket;
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
self.socket = [[AsyncUdpSocketalloc] initWithDelegate:self];
NSError *error = nil;
[self.socket bindToPort:3000 error:&error];
[self.socketenableBroadcast:YESerror:&error];
[self.socketjoinMulticastGroup:@"239.255.255.250"error:&error];
if (error) {
NSLog(@"error: %@",error);
}
[self.socketreceiveWithTimeout:-1tag:1];
NSLog(@"start udp server");
NSDictionary *ifs = [self fetchSSIDInfo]; //获取sid信息。
NSString *ssid = [[ifs objectForKey:@"SSID"] lowercaseString];
NSLog(@"ssid:%@",ssid);
_wifiName.text = [NSString stringWithFormat:@"Wifi name:\n%@",ssid];
}
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{
NSString *receiveData = [[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"received data: %@",receiveData);
_data.text = [NSString stringWithFormat:@"Received data:\n%@",receiveData];
[self.socket receiveWithTimeout:-1 tag:1];
returnYES;
}
- (id)fetchSSIDInfo
{
NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();
NSLog(@"%s: Supported interfaces: %@", __func__, ifs);
id info = nil;
for (NSString *ifnam in ifs) {
info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
if (info && [info count]) {
break;
}
}
return info ;
}