iOS gets WiFi ip, subnet mask, and domain transfer IP
Last Update:2017-06-28
Source: Internet
Author: User
<span id="Label3"></p><p><p>Get the header files needed for Wifi:</p></p><p><p>#import "GetCurrentIP.h"</p></p><p><p>#import <ifaddrs.h></p></p><p><p>#import <arpa/inet.h></p></p><p><p>#import <SystemConfiguration/CaptiveNetwork.h></p></p><p><p>#include <netdb.h></p></p><p><p>#include <net/if.h></p></p><p><p>#import <dlfcn.h></p></p><p><p>#include <sys/socket.h></p></p><p><p>#include <sys/sysctl.h></p></p><p><p>How to get the IP of the connected Wifi:</p></p><p><p>#pragma mark-gets The User's current IP address</p></p><p><p>+ (nullable nsstring*) Getcurrentlocalip</p></p><p><p>{</p></p><p><p>NSString *address = nil;</p></p><p><p>struct Ifaddrs *interfaces = NULL;</p></p><p><p>struct Ifaddrs *temp_addr = NULL;</p></p><p><p>int success = 0;</p></p><p><p>Retrieve the current Interfaces-returns 0 on success</p></p><p><p>Success = Getifaddrs (&interfaces);</p></p><p><p>if (success = = 0) {</p></p><p><p>Loop through linked list of interfaces</p></p><p><p>TEMP_ADDR = interfaces;</p></p><p><p>While (temp_addr! = NULL) {</p></p><p><p>if (temp_addr->ifa_addr->sa_family = = Af_inet) {</p></p><p><p>Check if interface is En0 which are the WiFi connection on the IPhone</p></p><p><p>If ([[[nsstring stringwithutf8string:temp_addr->ifa_name] isequaltostring:@ "en0"]) {</p></p><p><p>Get NSString from C String</p></p><p><p>Address = [nsstring Stringwithutf8string:inet_ntoa ((struct sockaddr_in *) temp_addr->ifa_addr->sin_addr)];</p></p><p><p>}</p></p><p><p>}</p></p><p><p>TEMP_ADDR = temp_addr->ifa_next;</p></p><p><p>}</p></p><p><p>}</p></p><p><p>Free memory</p></p><p><p>Freeifaddrs (interfaces);</p></p><p><p>Return address;</p></p><p><p>}</p></p><p><p>Get more information about your connected Wifi:</p></p><p><p>+ (nullable nsstring*) Getcurrentwifimessage {</p></p><p><p>NSString *address = nil;</p></p><p><p>struct Ifaddrs *interfaces = NULL;</p></p><p><p>struct Ifaddrs *temp_addr = NULL;</p></p><p><p>int success = 0;</p></p><p><p>Retrieve the current Interfaces-returns 0 on success</p></p><p><p>Success = Getifaddrs (&interfaces);</p></p><p><p>if (success = = 0)</p></p><p><p>{</p></p><p><p>Loop through linked list of interfaces</p></p><p><p>TEMP_ADDR = interfaces;</p></p><p><p>While (temp_addr! = NULL)</p></p><p><p>{</p></p><p><p>if (temp_addr->ifa_addr->sa_family = = Af_inet)</p></p><p><p>{</p></p><p><p>Check if interface is En0 which are the WiFi connection on the IPhone</p></p><p><p>If ([[[nsstring stringwithutf8string:temp_addr->ifa_name] isequaltostring:@ "en0"])</p></p><p><p>Address = [nsstring Stringwithutf8string:inet_ntoa ((struct sockaddr_in *) temp_addr->ifa_netmask->sin_addr)] ;</p></p><p><p>NSLog (@ "subnet mask:%@", [nsstring stringwithutf8string:inet_ntoa ((struct sockaddr_in *) temp_addr->ifa_n Etmask) (->sin_addr)]);</p></p><p><p>NSLog (@ "local ip:%@", [nsstring stringwithutf8string:inet_ntoa ((struct sockaddr_in *) Temp_addr->ifa_addr) ->sin_addr)]);</p></p><p><p>NSLog (@ "broadcast address:%@", [nsstring stringwithutf8string:inet_ntoa ((struct sockaddr_in *) Temp_addr->ifa_dstad dr)->sin_addr)]);</p></p><p><p>}</p></p><p><p></p></p><p><p>TEMP_ADDR = temp_addr->ifa_next;</p></p><p><p>}</p></p><p><p>}</p></p><p><p>Free memory</p></p><p><p>Freeifaddrs (interfaces);</p></p><p><p>Return address;</p></p><p><p>}</p></p><p><p>Domain name converted to Ip:</p></p><p><p>#pragma mark-how to convert a domain name to IP</p></p><p><p>+ (nsstring *) queryipwithdomain: (nsstring *) Domain</p></p><p><p>{</p></p><p><p>struct Hostent *hs;</p></p><p><p>struct sockaddr_in server;</p></p><p><p>If (hs = gethostbyname ([domain utf8string])! = NULL)</p></p><p><p>{</p></p><p><p>SERVER.SIN_ADDR = * (struct in_addr*) hs->h_addr_list[0]);</p></p><p><p>return [nsstring Stringwithutf8string:inet_ntoa (server.sin_addr)];</p></p><p><p>}</p></p><p><p>return @ "1";</p></p><p><p>}</p></p><p><p></p></p><p><p>iOS gets WiFi ip, subnet mask, and domain transfer IP</p></p></span>