您的位置:首页 > 其它

不调用苹果似有API也能获取 iPhone 本机 IP 地址的

2010-07-30 15:31 926 查看
在 OS 2.0 时代,我们可以使用以下方法获得 iPhone 的 IP 地址

NSString *ad = [myhost address];

if (ad)

strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]);

}

return [NSString stringWithFormat:@"%s",iphone_ip];

}
但是到了 OS 3.0,苹果将 IP 查询 API 设成了私有的,你要是还用上述方法,会因为使用苹果私有 API 而审核被拒。

下面是不使用苹果私有 API 获得 iPhone IP 地址的代码

/*

* IPAdress.h

*

*

*/



#define MAXADDRS 32



extern char *if_names[MAXADDRS];

extern char *ip_names[MAXADDRS];

extern char *hw_addrs[MAXADDRS];

extern unsigned long ip_addrs[MAXADDRS];



// Function prototypes



void InitAddresses();

void FreeAddresses();

void GetIPAddresses();

void GetHWAddresses();





/*

* IPAddress.c

*

*/



#include "IPAddress.h"



#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include



#define min(a,b) ((a) < (b) ? (a) : (b))

#define max(a,b) ((a) > (b) ? (a) : (b))



#define BUFFERSIZE 4000



char *if_names[MAXADDRS];

char *ip_names[MAXADDRS];

char *hw_addrs[MAXADDRS];

unsigned long ip_addrs[MAXADDRS];



static int nextAddr = 0;



void InitAddresses()

{

int i;

for (i=0; iifr_addr.sa_family != AF_INET)

{

continue; // ignore if not desired address family

}



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

{

*cptr = 0; // replace colon will null

}



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

{

continue; /* already processed this interface */

}



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



ifrcopy = *ifr;

ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);

flags = ifrcopy.ifr_flags;

if ((flags & IFF_UP) == 0)

{

continue; // ignore if interface not up

}



if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1);

if (if_names[nextAddr] == NULL)

{

return;

}

strcpy(if_names[nextAddr], ifr->ifr_name);



sin = (struct sockaddr_in *)&ifr->ifr_addr;

strcpy(temp, inet_ntoa(sin->sin_addr));



ip_names[nextAddr] = (char *)malloc(strlen(temp)+1);

if (ip_names[nextAddr] == NULL)

{

return;

}

strcpy(ip_names[nextAddr], temp);



ip_addrs[nextAddr] = sin->sin_addr.s_addr;



++nextAddr;

}



close(sockfd);

}



void GetHWAddresses()

{

struct ifconf ifc;

struct ifreq *ifr;

int i, sockfd;

char buffer[BUFFERSIZE], *cp, *cplim;

char temp[80];



for (i=0; i
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: