美文网首页wifi开发
iOS开发中WiFi相关功能总结

iOS开发中WiFi相关功能总结

作者: keenteam | 来源:发表于2017-10-19 10:46 被阅读173次

    1.获取WiFi信息

    以前物联网刚火的时候,出现过很多一体式无线路由,所以App里难免会遇到要判断当前所连接的WiFi,以及获取WiFi信息的功能。13年的时候查过一些关于WiFi的方法,后面渐渐都忘记了。惭愧!!!

    需要添加SystemConfiguration.framework并在当前类中添加代码#import<SystemConfiguration/CaptiveNetwork.h>

    //获取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;

    }

    //打印出来的结果:

    2016-05-12 15:28:51.674 SimplePing[18883:6790207] WIFI_INFO:{

    BSSID = "a4:2b:8c:c:7f:bd";

    SSID = bdmy06;

    SSIDDATA = <73756e65 65653036>;

    }

    2.获取WiFi名称

    NSString *WiFiName = info[@"SSID"];

    //打印结果:

    2016-05-12 15:35:13.059 SimplePing[18887:6791418] bdmy06

    完整的:

    - (NSString *)fetchWiFiName {

    NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();

    if (!ifs) {

    return nil;

    }

    NSString *WiFiName = nil;

    for (NSString *ifnam in ifs) {

    NSDictionary *info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);

    if (info && [info count]) {

    // 这里其实对应的有三个key:kCNNetworkInfoKeySSID、kCNNetworkInfoKeyBSSID、kCNNetworkInfoKeySSIDData,

    // 不过它们都是CFStringRef类型的

    WiFiName = [info objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];

    //            WiFiName = [info objectForKey:@"SSID"];

    break;

    }

    }

    return WiFiName;

    }

    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.1.255 广播地址

    NSLog(@"broadcast address--%@",[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)]);

    //--192.168.1.106 本机地址

    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;

    }

    4.获取本机在WiFi环境下的IP地址

    - (NSString *)getLocalIPAddressForCurrentWiFi

    {

    NSString *address = nil;

    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) {

    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"]) {

    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];

    return address;

    }

    }

    temp_addr = temp_addr->ifa_next;

    }

    freeifaddrs(interfaces);

    }

    return nil;

    }

    相关文章

      网友评论

        本文标题:iOS开发中WiFi相关功能总结

        本文链接:https://www.haomeiwen.com/subject/ddcuuxtx.html