• IOS开发之WIFI及IP相关


    获取手机设备的IP信息:

    #import <ifaddrs.h>

    #import <arpa/inet.h>

                           // Get IP Address

    - (NSString *)getIPAddress {

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

                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

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

                    }

                }

                temp_addr = temp_addr->ifa_next;

            }

        }

        // Free memory

        freeifaddrs(interfaces);

        return address;

    }

     

         //  Get SSID

    #import <SystemConfiguration/CaptiveNetwork.h>

    - (NSString *)currentWifiSSID

    {

        NSString *ssid = nil;

        NSArray *ifs = (__bridge   id)CNCopySupportedInterfaces();

        for (NSString *ifname in ifs) {

            NSDictionary *info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifname);

            if (info[@"SSID"])

            {

                ssid = info[@"SSID"];

            }

        }

        return ssid;

    }

    #import <sys/socket.h>

    #import <sys/sockio.h>

    #import <sys/ioctl.h>

    #import <net/if.h>

    #import <arpa/inet.h>

    - (NSString *)getDeviceIPIpAddresses

    {

        

        int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

        if ( sockfd < 0) return nil;

        

        NSMutableArray *ips = [NSMutableArray array];

        

        

        

        int BUFFERSIZE = 4096;

        

        struct ifconf ifc;

        

        char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;

        

        struct ifreq *ifr, ifrcopy;

        

        ifc.ifc_len = BUFFERSIZE;

        

        ifc.ifc_buf = buffer;

        if (ioctl(sockfd, SIOCGIFCONF, &ifc) >= 0){

            

            for (ptr = buffer; ptr < buffer + ifc.ifc_len; ){

                

                ifr = (struct ifreq *)ptr;

                

                int len = sizeof(struct sockaddr);

                

                if (ifr->ifr_addr.sa_len > len) {

                    

                    len = ifr->ifr_addr.sa_len;

                    

                }

                

                ptr += sizeof(ifr->ifr_name) + len;

                

                if (ifr->ifr_addr.sa_family != AF_INET) continue;

                

                if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL) *cptr = 0;

                

                if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) continue;

                

                memcpy(lastname, ifr->ifr_name, IFNAMSIZ);

                ifrcopy = *ifr;

                

                ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);

                

                if ((ifrcopy.ifr_flags & IFF_UP) == 0) continue;

                NSString *ip = [NSString  stringWithFormat:@"%s", inet_ntoa(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr)];

                

                [ips addObject:ip];

                

            }

            

        }

        

        close(sockfd);

        NSString *deviceIP = @"";

        

        for (int i=0; i < ips.count; i++)

            

        {

            

            if (ips.count > 0)

                

            {

                

                deviceIP = [NSString stringWithFormat:@"%@",ips.lastObject];

                

                

                

            }

            

        }

        

        return deviceIP;

        

    }

    提高技能如同提升自信心。
  • 相关阅读:
    RocketMQ性能压测分析(转载)
    利用Fiddler或Charles进行mock数据
    Linux中buffer/cache,swap,虚拟内存和page ++
    AVA 8 :从永久区(PermGen)到元空间(Metaspace)
    jstat 监控调整GC很好用
    Jmeter常用函数
    关于Oracle新建表空间,添加用户及新建表数据
    关于Oracle增加表空间大小方法
    关于手动删除Oracle数据数据,导致Oracle无法连接处理过程
    解决jquery easyui combotree(下拉树)点击文字无法展开下级菜单的解决方法
  • 原文地址:https://www.cnblogs.com/chims-liu-touch/p/5736572.html
Copyright © 2020-2023  润新知