iphone - ios get ip address code worked before but failed now -
i use code below ip address of iphone
#define ios_cellular @"pdp_ip0" #define ios_wifi @"en0" #define ios_vpn @"utun0" #define ip_addr_ipv4 @"ipv4" #define ip_addr_ipv6 @"ipv6" - (nsstring *) getipaddress1; { nsstring *address = @"error"; struct ifaddrs *interfaces = null; struct ifaddrs *temp_addr = null; int success = 0; // retrieve 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 en0 wifi connection on iphone nsstring *s= [nsstring stringwithutf8string:temp_addr->ifa_name]; nslog(@"%@ ---",s); if([s isequaltostring:ios_wifi] || [s isequaltostring:ios_cellular] || [s isequaltostring:ios_vpn]) { // nsstring 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; }
it worked before, returns @"error"
.
i tried method mention here
iphone/ipad/osx: how ip address programmatically?
but none workd
your comment welcome
try
#import <ifaddrs.h> #import <arpa/inet.h> - (nsstring *)getipaddress { nsstring *address = @"error"; struct ifaddrs *interfaces = null; struct ifaddrs *temp_addr = null; int success = 0; // retrieve 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 en0 wifi connection on iphone if([[nsstring stringwithutf8string:temp_addr->ifa_name] isequaltostring:@"en0"]) { // nsstring 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;
}
Comments
Post a Comment