There are two ways to do this:
1 //get the native IP2-(NSString *) LocalIpAddress3 {4NSString *localip =Nil;5 structIfaddrs *Addrs;6 if(Getifaddrs (&addrs) = =0) {7 Const structIfaddrs *cursor =Addrs;8 while(Cursor! =NULL) {9 if(cursor->ifa_addr->sa_family = = Af_inet && (cursor->ifa_flags & iff_loopback) = =0)Ten { One { ALocalip = [NSString Stringwithutf8string:inet_ntoa ((structsockaddr_in *) cursor->ifa_addr)sin_addr)]; - Break; - } the } -cursor = cursor->Ifa_next; - } - Freeifaddrs (Addrs); + } - returnLocalip; +}
1 //get the native IP address2-(NSString *) getipaddress3 {4NSString *address =@"Error";5 structIfaddrs *interfaces =NULL;6 structIfaddrs *temp_addr =NULL;7 intSuccess =0;8 9 //retrieve the current interfaces-returns 0 on successTenSuccess = Getifaddrs (&interfaces); One if(Success = =0) { A //Loop through linked list of interfaces -TEMP_ADDR =interfaces; - while(Temp_addr! =NULL) { the 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 ((structsockaddr_in *) temp_addr->ifa_addr)sin_addr)]; - } + } A atTEMP_ADDR = temp_addr->Ifa_next; - } - } - - //Free Memory - Freeifaddrs (interfaces); in - returnaddress; to}
Get the native IP address