首先先列一下获取SSID的代码:OC
/**
获取WiFi信息
@return WiFi MAC地址
*/
+(NSString *)fetchBSSIDInfo{
NSString*currentSSID =@"Not Found";
CFArrayRef myArray = CNCopySupportedInterfaces();
if(myArray !=nil){
NSDictionary* myDict = (__bridge NSDictionary *) CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0)); if(myDict!=nil){
currentSSID = [myDictvalueForKey:@"BSSID"];
}else{
currentSSID=@"<<NONE>>";
}
}else{
currentSSID=@"<<NONE>>";
}
NSArray*array = [currentSSIDcomponentsSeparatedByString:@":"];
NSMutableArray*ssidArray = [[NSMutableArrayalloc]initWithArray:array];
for(NSIntegeri=0; i
NSString*str = array[i];
NSString*charStr;
if(str.length==1) {
charStr = [NSStringstringWithFormat:@"0%@",str];
[ssidArrayreplaceObjectAtIndex:iwithObject:charStr];
}
}
NSString*ssid =@"";
for(NSIntegeri=0; i
NSString*str;
if(i == ssidArray.count-1) {
str = [NSStringstringWithFormat:@"%@",ssidArray[i]];
}else{
str = [NSStringstringWithFormat:@"%@:",ssidArray[i]];
}
ssid = [ssidstringByAppendingString:str];
}
if([UtilityisEmpty:ssid]) {
ssid =@"<<NONE>>";
}
returnssid;
}
/**
获取WiFi信息
@return WiFi名称
*/
+(NSString *)fetchSSIDInfo{
NSString*currentSSID =@"Not Found";
CFArrayRef myArray = CNCopySupportedInterfaces();
if(myArray !=nil){
NSDictionary* myDict = (__bridge NSDictionary *) CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0)); if(myDict!=nil){
currentSSID=[myDictvalueForKey:@"SSID"];
}else{
currentSSID=@"<<NONE>>";
}
}else{
currentSSID=@"<<NONE>>";
}
return currentSSID;
}
但是在iOS 12.0 + 之后,苹果官方增加了获取wifi的权限,具体说明可以查看 CNCopyCurrentNetworkInfo 的官方文档说明
打开开关的方式有两种:
1 )Xcode11.1
这种方式最简单,Xcode -> Targets -> Capabilities,如下图所示,直接点击打开
打开之后Xcode也会同步到AppID的设置
( 2 )Apple Developer(苹果开发者中心)
操作步骤如下图:(用第一种方法(Xcode)操作的话,Xcode也会自动同步状态到开发者中心对应AppID的开关的)
网友评论