[转]取iphone本机ip地址方法(非调用私有api方法)
转自:http://hi.baidu.com/%B0%D9ii%D6%AAi%B5%C0/blog/item/5d4c0a2b284e5fea98250a41.html
手头一个iphone项目需要取iphone本机ip地址,在iphone os 2.0上可以用下面的方法获得:
-(NSString*)getAddress{
chariphone_ip[255];
strcpy(iphone_ip,"127.0.0.1"); // if everything fails
NSHost* myhost = [NSHostcurrentHost];
if(myhost)
{
NSString *ad = [myhostaddress];
if(ad)
strcpy(iphone_ip,[adcStringUsingEncoding: NSISOLatin1StringEncoding]);
}
return[NSStringstringWithFormat:@"%s",iphone_ip];
}
chariphone_ip[255];
strcpy(iphone_ip,"127.0.0.1"); // if everything fails
NSHost* myhost = [NSHostcurrentHost];
if(myhost)
{
NSString *ad = [myhostaddress];
if(ad)
strcpy(iphone_ip,[adcStringUsingEncoding: NSISOLatin1StringEncoding]);
}
return[NSStringstringWithFormat:@"%s",iphone_ip];
}
到3.0这个方法成了苹果私有api了,用了不对不说,error:
warning: no ‘+currentHost’ method found
warning: (Messages without a matching method signature)
,提交的app还被reject:
[NSHost currentHost] will also work, but it is deprecated and considered a “Private API” by Apple, so you won’t be able to submit your application to App Store.
google很久无果;
今天无意发现一个老外的blog贴了方法,试用了下完全OK,要FQ看,转载记录一下.
As far as I know there is only one hacky way to do that. You basically open a socket and get its address using POSIX functions. Here is the code I used for this:
/*
* IPAdress.h
*
*
*/
#defineMAXADDRS 32
externchar *if_names[MAXADDRS];
externchar *ip_names[MAXADDRS];
externchar *hw_addrs[MAXADDRS];
externunsignedlongip_addrs[MAXADDRS];
// Function prototypes
voidInitAddresses();
voidFreeAddresses();
voidGetIPAddresses();
voidGetHWAddresses();
/*
* IPAddress.c
*
*/
#include "IPAddress.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <errno.h>
#include <net/if_dl.h>
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
#defineBUFFERSIZE 4000
char *if_names[MAXADDRS];
char *ip_names[MAXADDRS];
char *hw_addrs[MAXADDRS];
unsignedlongip_addrs[MAXADDRS];
staticint nextAddr = 0;
voidInitAddresses()
{
inti;
for(i=0; i<MAXADDRS; ++i)
{
if_names[i] = ip_names[i] = hw_addrs[i] = NULL;
ip_addrs[i] = 0;
}
}
voidFreeAddresses()
{
inti;
for(i=0; i<MAXADDRS; ++i)
{
if(if_names[i] != 0)free(if_names[i]);
if(ip_names[i] != 0)free(ip_names[i]);
if(hw_addrs[i] != 0)free(hw_addrs[i]);
ip_addrs[i] = 0;
}
InitAddresses();
}
* IPAdress.h
*
*
*/
#defineMAXADDRS 32
externchar *if_names[MAXADDRS];
externchar *ip_names[MAXADDRS];
externchar *hw_addrs[MAXADDRS];
externunsignedlongip_addrs[MAXADDRS];
// Function prototypes
voidInitAddresses();
voidFreeAddresses();
voidGetIPAddresses();
voidGetHWAddresses();
/*
* IPAddress.c
*
*/
#include "IPAddress.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <errno.h>
#include <net/if_dl.h>
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
#defineBUFFERSIZE 4000
char *if_names[MAXADDRS];
char *ip_names[MAXADDRS];
char *hw_addrs[MAXADDRS];
unsignedlongip_addrs[MAXADDRS];
staticint nextAddr = 0;
voidInitAddresses()
{
inti;
for(i=0; i<MAXADDRS; ++i)
{
if_names[i] = ip_names[i] = hw_addrs[i] = NULL;
ip_addrs[i] = 0;
}
}
voidFreeAddresses()
{
inti;
for(i=0; i<MAXADDRS; ++i)
{
if(if_names[i] != 0)free(if_names[i]);
if(ip_names[i] != 0)free(ip_names[i]);
if(hw_addrs[i] != 0)free(hw_addrs[i]);
ip_addrs[i] = 0;
}
InitAddresses();
}
voidGetIPAddresses()
{
int i, len, flags;
char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
structifconf ifc;
structifreq *ifr, ifrcopy;
structsockaddr_in *sin;
chartemp[80];
intsockfd;
for(i=0; i<MAXADDRS; ++i)
{
if_names[i] = ip_names[i] = NULL;
ip_addrs[i] = 0;
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
perror("socket failed");
return;
}
ifc.ifc_len = BUFFERSIZE;
ifc.ifc_buf = buffer;
if(ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
{
perror("ioctl error");
return;
}
lastname[0] = 0;
for(ptr = buffer; ptr < buffer + ifc.ifc_len; )
{
ifr = (structifreq *)ptr;
len = max(sizeof(structsockaddr), ifr->ifr_addr.sa_len);
ptr += sizeof(ifr->ifr_name) + len; // for next one in buffer
if(ifr->ifr_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 = (structsockaddr_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);
}
{
int i, len, flags;
char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
structifconf ifc;
structifreq *ifr, ifrcopy;
structsockaddr_in *sin;
chartemp[80];
intsockfd;
for(i=0; i<MAXADDRS; ++i)
{
if_names[i] = ip_names[i] = NULL;
ip_addrs[i] = 0;
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
perror("socket failed");
return;
}
ifc.ifc_len = BUFFERSIZE;
ifc.ifc_buf = buffer;
if(ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
{
perror("ioctl error");
return;
}
lastname[0] = 0;
for(ptr = buffer; ptr < buffer + ifc.ifc_len; )
{
ifr = (structifreq *)ptr;
len = max(sizeof(structsockaddr), ifr->ifr_addr.sa_len);
ptr += sizeof(ifr->ifr_name) + len; // for next one in buffer
if(ifr->ifr_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 = (structsockaddr_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);
}
voidGetHWAddresses()
{
structifconfifc;
structifreq *ifr;
inti, sockfd;
charbuffer[BUFFERSIZE], *cp, *cplim;
chartemp[80];
for(i=0; i<MAXADDRS; ++i)
{
hw_addrs[i] = NULL;
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
perror("socket failed");
return;
}
ifc.ifc_len = BUFFERSIZE;
ifc.ifc_buf = buffer;
if(ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0)
{
perror("ioctl error");
close(sockfd);
return;
}
ifr = ifc.ifc_req;
cplim = buffer + ifc.ifc_len;
for(cp=buffer; cp < cplim; )
{
ifr = (structifreq *)cp;
if(ifr->ifr_addr.sa_family == AF_LINK)
{
structsockaddr_dl *sdl = (structsockaddr_dl *)&ifr->ifr_addr;
inta,b,c,d,e,f;
inti;
strcpy(temp, (char *)ether_ntoa(LLADDR(sdl)));
sscanf(temp, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);
sprintf(temp, "%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);
for(i=0; i<MAXADDRS; ++i)
{
if((if_names[i] != NULL) && (strcmp(ifr->ifr_name, if_names[i]) == 0))
{
if(hw_addrs[i] == NULL)
{
hw_addrs[i] = (char *)malloc(strlen(temp)+1);
strcpy(hw_addrs[i], temp);
break;
}
}
}
}
cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);
}
close(sockfd);
}
{
structifconfifc;
structifreq *ifr;
inti, sockfd;
charbuffer[BUFFERSIZE], *cp, *cplim;
chartemp[80];
for(i=0; i<MAXADDRS; ++i)
{
hw_addrs[i] = NULL;
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
perror("socket failed");
return;
}
ifc.ifc_len = BUFFERSIZE;
ifc.ifc_buf = buffer;
if(ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0)
{
perror("ioctl error");
close(sockfd);
return;
}
ifr = ifc.ifc_req;
cplim = buffer + ifc.ifc_len;
for(cp=buffer; cp < cplim; )
{
ifr = (structifreq *)cp;
if(ifr->ifr_addr.sa_family == AF_LINK)
{
structsockaddr_dl *sdl = (structsockaddr_dl *)&ifr->ifr_addr;
inta,b,c,d,e,f;
inti;
strcpy(temp, (char *)ether_ntoa(LLADDR(sdl)));
sscanf(temp, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);
sprintf(temp, "%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);
for(i=0; i<MAXADDRS; ++i)
{
if((if_names[i] != NULL) && (strcmp(ifr->ifr_name, if_names[i]) == 0))
{
if(hw_addrs[i] == NULL)
{
hw_addrs[i] = (char *)malloc(strlen(temp)+1);
strcpy(hw_addrs[i], temp);
break;
}
}
}
}
cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);
}
close(sockfd);
}
test:
#import"IPAdress.h"
..........
- (NSString *)deviceIPAdress{
InitAddresses();
GetIPAddresses();
GetHWAddresses();
return[NSStringstringWithFormat:@"%s", ip_names[1]];
}
- (void)viewDidLoad{
[superviewDidLoad];
NSString* ip_iphone = [selfdeviceIPAdress];
NSLog(@"ip:%@",ip_iphone);
}
..........
- (NSString *)deviceIPAdress{
InitAddresses();
GetIPAddresses();
GetHWAddresses();
return[NSStringstringWithFormat:@"%s", ip_names[1]];
}
- (void)viewDidLoad{
[superviewDidLoad];
NSString* ip_iphone = [selfdeviceIPAdress];
NSLog(@"ip:%@",ip_iphone);
}
原代码地址(需FQ):http://iphonesdksnippets.com/post/2009/09/07/Get-IP-address-of-iPhone.aspx