找资料的时候看到别人写的,记录下,有空自己研究下
iOS下Wi-Fi开发需要添加依赖库SystemConfiguration.framework,在需要使用Wi-Fi信息的控制器下引入头文件#import
1,Wi-Fi热点获取
+(NSString *) currentWifiSSID
{
if TARGET_OS_SIMULATOR
return @"(simulator)";
else
NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();
id info = nil;
for (NSString *ifnam in ifs) {
info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
if (info && [info count]) {
break;
}
}
NSDictionary *dctySSID = (NSDictionary *)info;
NSString *ssid = [dctySSID objectForKey:@"SSID"] ;
return ssid;
endif
}
2,取得本机Wi-Fi的IP地址
include
include
include
include
import
+(NSString *) localWiFiIPAddress
{
BOOL success;
struct ifaddrs * addrs;
const struct ifaddrs * cursor;
success = getifaddrs(&addrs) == 0;
if (success) {
cursor = addrs;
while (cursor != NULL) {
// the second test keeps from picking up the loopback address
if (cursor->ifa_addr->sa_family == AF_INET && (cursor->ifa_flags & IFF_LOOPBACK) == 0)
{
NSString *name = [NSString stringWithUTF8String:cursor->ifa_name];
if ([name isEqualToString:@"en0"]) // Wi-Fi adapter
return [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)];
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
return nil;
}
3,获取当前Wi-Fi的名称
- (void)GetWifiName{
NSString *wifiName = @"Not Found";
CFArrayRef myArray = CNCopySupportedInterfaces();
if (myArray != nil) {
CFDictionaryRef myDict =CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
if (myDict != nil) {
NSDictionary dict = (NSDictionary)CFBridgingRelease(myDict);
wifiName = [dict valueForKey:@"SSID"];
}
NSLog(@"wifiName:%@", wifiName);
}
注意:需要真机才能测试!模拟器下返回空。
4,打开系统设置
从应用跳到系统设置里的WiFi界面有这个需求存在吗?答案是肯定的。
在没有网的状态下,你可能想提醒用户去设置界面连接WiFi。如果不能跳到WiFi界面,只能在APP里面做出文字提示。这样很多小白用户可能不会看提示,只会觉得APP没有做好。
+(void)openSystemSetting:(NSString *)settingName{
//iOS8 才有效
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
define SETTING_URL @"app-settings:"
else
define SETTING_URL UIApplicationOpenSettingsURLString
endif
// NSLog(UIApplicationOpenSettingsURLString);
if (version >= 8.0){
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString:SETTING_URL]];
}
}
UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root=WIFI")!)
原文链接地址:http://www.cnblogs.com/xjf125/p/5360550.html