2 xingfudeyutian xingfudeyutian 于 2014.11.30 17:40 提问

iOS中如何获取wifi的路由器地址

iOS开发中,手机连接wifi,怎么获取wifi或网关的路由器的地址

3个回答

u010990519
u010990519   2014.12.02 15:00
已采纳
xingfudeyutian
xingfudeyutian 弄了好久,这个就是我想要的地址了~
大约 3 年之前 回复
Rocloud
Rocloud   Rxr 2014.11.30 17:51

转载,或许可以参考,ios设备唯一标识获取策略

iOS中获取网卡mac的代码如下:
#include // Per msqr

#include

#include

#include

#pragma mark MAC addy

// Return the local MAC addy

// Courtesy of FreeBSD hackers email list

// Accidentally munged during previous update. Fixed thanks to mlamb.

  • (NSString *) macaddress
    {
    int mib[6];
    size_t len;
    char *buf;
    unsigned char *ptr;
    struct if_msghdr *ifm;
    struct sockaddr_dl *sdl;

    mib[0] = CTL_NET;
    mib1 = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;

    if ((mib[5] = if_nametoindex("en0")) == 0) {
    printf("Error: if_nametoindex error/n");
    return NULL;
    }

    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
    printf("Error: sysctl, take 1/n");
    return NULL;
    }

    if ((buf = malloc(len)) == NULL) {
    printf("Could not allocate memory. error!/n");
    return NULL;
    }

    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
    printf("Error: sysctl, take 2");
    return NULL;
    }

    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    // NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];

    NSString *outstring = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    free(buf);
    return [outstring uppercaseString];
    }

u011757360
u011757360   2014.11.30 22:54

很感兴趣,是不是得查一下路由表相关的东西?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!