iOS gets WiFi ip, subnet mask, and domain transfer IP

Source: Internet
Author: User
Tags domain transfer
<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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.