IOS gets the IP address of the network that the phone is currently connected to

Source: Internet
Author: User

1 first to import these header files in the current class:

#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#include <ifaddrs.h>
#import <dlfcn.h>

2 implementation of the specific code:

Get the IP address of the phone's network
-(NSString *) getipaddress
{
BOOL success;
struct Ifaddrs * ADDRS;
const struct IFADDRS * CURSOR;
Success = Getifaddrs (&addrs) = = 0;
if (success) {
cursor = Addrs;
while (cursor! = NULL) {
The second test keeps from picking up the loopback address
if (cursor->ifa_addr->sa_family = = Af_inet && (cursor->ifa_flags & iff_loopback) = = 0)
{
NSString *name = [NSString stringwithutf8string:cursor->ifa_name];
if ([Name isequaltostring:@ "En0"])//Wi-Fi Adapter
NSLog (@ "ip:%@", [NSString Stringwithutf8string:inet_ntoa ((struct sockaddr_in *) cursor->ifa_addr) ]);
return [NSString Stringwithutf8string:inet_ntoa ((struct sockaddr_in *) cursor->ifa_addr->sin_addr)];
}
cursor = cursor->ifa_next;
}
Freeifaddrs (Addrs);
}
return nil;
}

IOS gets the IP address of the network that the phone is currently connected to

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.