Get IP information for your mobile device:
#import <ifaddrs.h>
#import <arpa/inet.h>
Get IP Address
-(NSString *) getipaddress {
NSString *address = @ "error";
struct Ifaddrs *interfaces = NULL;
struct Ifaddrs *temp_addr = NULL;
int success = 0;
Retrieve the current Interfaces-returns 0 on success
Success = Getifaddrs (&interfaces);
if (success = = 0) {
Loop through linked list of interfaces
TEMP_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 String
address = [NSString stringwithutf8string:inet_ntoa ((struct sockaddr_in *) temp_addr->ifa_addr->sin_addr)];
}
}
TEMP_ADDR = temp_addr->ifa_next;
}
}
Free memory
Freeifaddrs (interfaces);
return address;
}
Get SSID
#import <SystemConfiguration/CaptiveNetwork.h>
-(NSString *) currentwifissid
{
NSString *ssid = nil;
Nsarray *ifs = (__bridge ID) cncopysupportedinterfaces ();
For (NSString *ifname in IFS) {
Nsdictionary *info = (__bridge ID) cncopycurrentnetworkinfo ((__bridge cfstringref) ifname);
if (info[@ "SSID"])
{
SSID = info[@ "SSID"];
}
}
return SSID;
}
#import <sys/socket.h>
#import <sys/sockio.h>
#import <sys/ioctl.h>
#import <net/if.h>
#import <arpa/inet.h>
-(NSString *) getdeviceipipaddresses
{
int SOCKFD = socket (af_inet, SOCK_DGRAM, 0);
if (SOCKFD < 0) return nil;
Nsmutablearray *ips = [Nsmutablearray array];
int buffersize = 4096;
struct ifconf ifc;
Char Buffer[buffersize], *ptr, Lastname[ifnamsiz], *cptr;
struct Ifreq *ifr, ifrcopy;
Ifc.ifc_len = buffersize;
ifc.ifc_buf = buffer;
if (IOCTL (SOCKFD, siocgifconf, &IFC) >= 0) {
for (ptr = buffer; ptr < buffer + Ifc.ifc_len;) {
IFR = (struct ifreq *) ptr;
int len = sizeof (struct sockaddr);
if (Ifr->ifr_addr.sa_len > Len) {
Len = ifr->ifr_addr.sa_len;
}
PTR + = sizeof (ifr->ifr_name) + len;
if (ifr->ifr_addr.sa_family! = af_inet) Continue;
if (cptr = (char *) STRCHR (ifr->ifr_name, ': '))! = NULL) *cptr = 0;
if (strncmp (LastName, Ifr->ifr_name, ifnamsiz) = = 0) continue;
memcpy (LastName, Ifr->ifr_name, Ifnamsiz);
Ifrcopy = *IFR;
IOCTL (SOCKFD, Siocgifflags, &ifrcopy);
if ((Ifrcopy.ifr_flags & iff_up) = = 0) continue;
NSString *ip = [NSString stringwithformat:@ "%s", Inet_ntoa (((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr )];
[IPs ADDOBJECT:IP];
}
}
Close (SOCKFD);
NSString *deviceip = @ "";
for (int i=0; i < ips.count; i++)
{
if (Ips.count > 0)
{
DEVICEIP = [NSString stringwithformat:@ "%@", Ips.lastobject];
}
}
return DEVICEIP;
}
WiFi and IP related to iOS development