Import the following header file # include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>#include<unistd.h>#include<sys/ioctl.h>#include<sys/types.h>#include<sys/sysctl.h>#include<sys/socket.h>#include<netinet/inch.h>#include<netdb.h>#include<arpa/inet.h>#include<sys/sockio.h>#include<net/if.h>#include<net/ethernet.h>#include<errno.h>#include<net/if_dl.h>#include<ifaddrs.h>#include<mach/machine.h>
The IP address can be obtained by the following method
+ (NSString *) getipaddress {nsstring*address =@"Error"; structIfaddrs *interfaces =NULL; structIfaddrs *temp_addr =NULL; intSuccess =0; //retrieve the current interfaces-returns 0 on successSuccess = Getifaddrs (&interfaces); if(Success = =0) { //Loop through linked list of interfacesTEMP_ADDR =interfaces; while(Temp_addr! =NULL) { if(temp_addr->ifa_addr->sa_family = =af_inet) { //Check If interface is En0 which are the WiFi connection on the IPhone if([[NSString Stringwithutf8string:temp_addr->ifa_name] isequaltostring:@"En0"]) { //Get nsstring from C Stringaddress = [NSString Stringwithutf8string:inet_ntoa ((structsockaddr_in *) temp_addr->ifa_addr)sin_addr)]; }} temp_addr= temp_addr->Ifa_next; } } //Free MemoryFreeifaddrs (interfaces); returnaddress;}
iOS gets the IP address of the device