1 first to import these header files in the current class:
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#include <ifaddrs.h>
#import <dlfcn.h>
2 implementation of the specific code:
Get the IP address of the phone's network
-(NSString *) getipaddress
{
BOOL success;
struct Ifaddrs * ADDRS;
const struct IFADDRS * CURSOR;
Success = Getifaddrs (&addrs) = = 0;
if (success) {
cursor = Addrs;
while (cursor! = NULL) {
The second test keeps from picking up the loopback address
if (cursor->ifa_addr->sa_family = = Af_inet && (cursor->ifa_flags & iff_loopback) = = 0)
{
NSString *name = [NSString stringwithutf8string:cursor->ifa_name];
if ([Name isequaltostring:@ "En0"])//Wi-Fi Adapter
NSLog (@ "ip:%@", [NSString Stringwithutf8string:inet_ntoa ((struct sockaddr_in *) cursor->ifa_addr) ]);
return [NSString Stringwithutf8string:inet_ntoa ((struct sockaddr_in *) cursor->ifa_addr->sin_addr)];
}
cursor = cursor->ifa_next;
}
Freeifaddrs (Addrs);
}
return nil;
}
IOS gets the IP address of the network that the phone is currently connected to