How to obtain mobile phone extranet IP address (with intranet IP address)

Source: Internet
Author: User

Looked for some methods, initially thought to get the phone is the public network address, in fact, only the local IP address. The following method to obtain the IP address of the mobile phone inside and outside the network summary:

First, obtain the mobile phone local static IP address: (Local area network)

Method 1:

First Import header file:

IP address Demand Library
#import <sys/socket.h>
#import <sys/sockio.h>
#import <sys/ioctl.h>
# Import <net/if.h>
#import <arpa/inet.h>
Specific methods:

Get device IP address-(NSString *) getdeviceipaddresses {int SOCKFD = socket (af_inet,sock_dgram, 0); if (sockfd <</span> 0) return nil;

    This sentence, due to reprint, do not understand, comment off no effect, understand the great God welcome guidance Nsmutablearray *ips = [Nsmutablearray array];
    
    int buffersize = 4096;
    
    struct ifconf ifc;
    
    Char Buffer[buffersize], *ptr, Lastname[ifnamsiz], *cptr;
    
    struct Ifreq *ifr, ifrcopy;
    
    Ifc.ifc_len = buffersize;
    
    ifc.ifc_buf = buffer; if (IOCTL (sockfd,siocgifconf, &AMP;IFC) >= 0) {for (ptr = buffer; ptr < buffer + Ifc.ifc_len;)
            
            {IFR = (struct ifreq *) ptr;
            
            int len =sizeof (struct sockaddr);
            if (Ifr->ifr_addr.sa_len > len) {len = ifr->ifr_addr.sa_len;
            
            PTR + + sizeof (ifr->ifr_name) + len;
            
            if (ifr->ifr_addr.sa_family!=af_inet) continue; if (cptr = (cHar *) strchr (ifr->ifr_name, ': '))!= NULL) *cptr = 0;
            
            if (strncmp (LastName, ifr->ifr_name,ifnamsiz) = = 0) continue;
            
            memcpy (LastName, Ifr->ifr_name,ifnamsiz);
            
            Ifrcopy = *IFR;
            
            IOCTL (Sockfd,siocgifflags, &ifrcopy);

            if ((ifrcopy.ifr_flags &iff_up) = = 0) continue; NSString *ip = [nsstring stringwithformat:@ '%s ', Inet_ntoa ((struct sockaddr_in *) &ifr->ifr_addr)
            )];
        [IPs ADDOBJECT:IP];

    } close (SOCKFD);
    
    NSString *deviceip =@ ""; for (int i=0 i < Ips.count i++) {if (Ips.count >0) {deviceip = [NSString stringwithformat:@]%@
        ", Ips.lastobject];
} return DEVICEIP; }

Reference: http://blog.sina.com.cn/s/blog_b0b335e20102y3im.html


Method 2:

Import two header files, this method can only get to the WiFi environment of the local IP, relatively simple

#import <ifaddrs.h>
#import <arpa/inet.h>
+ (NSString *) deviceipadress {
    NSString *address = @ "mobile mobile network";
    struct Ifaddrs *interfaces = NULL;
    struct Ifaddrs *temp_addr = NULL;
    int success = 0;
    
    Success = Getifaddrs (&interfaces);
    if (success = = 0) {
        temp_addr = interfaces;
        while (temp_addr!= NULL) {
            if (*temp_addr). ifa_addr->sa_family = = af_inet) {
                if ([[NSString Stringwithutf8string:temp_addr->ifa_name] isequaltostring:@ "En0") {address
                    = [NSString Stringwithutf8string:inet_ntoa ((struct sockaddr_in *) temp_addr->ifa_addr)->sin_addr)];
                }
            
            TEMP_ADDR = temp_addr->ifa_next;
        }
    }
    Freeifaddrs (interfaces);    NSLog (@ "Mobile IP is:%@", address);
    
    return address;


Second, access to mobile phone outside the network IP (public network IP)

Online search for a long time to get the IP method, a lot of access to the Web site has been unable to use, can use the main 2, but get to the IP address is different, the following detailed description.

The most recommended Method 1: This method used by the Taobao site, access to IP and Baidu IP is the same, taking into account today's rampant Baidu, so I use this method.

-(NSString *) devicewanipaddress
{
    Nsurl *ipurl = [Nsurl urlwithstring:@] http://ip.taobao.com/service/ Getipinfo.php?ip=myip "];
    NSData *data = [NSData Datawithcontentsofurl:ipurl];
    Nsdictionary *ipdic = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil] ;
    nsstring *ipstr = nil;
    if (ipdic && [ipdic[@ "code"] integervalue] = = 0) {//Get success
        ipstr = ipdic[@ "Data"][@ "IP";
    }
    Return (Ipstr ipstr: @ "");
}

the JSON data returned by the Access interface :


Baidu IP to find the native IP:



Method 2: This method access to the Sohu IP interface, the return of IP and Baidu Taobao is not the same. (Perhaps this interface is accurate to the specific area ...) )

-(NSString *) getwanipaddress
{
    nserror *error;
    Nsurl *ipurl = [Nsurl urlwithstring:@ "Http://pv.sohu.com/cityjson?ie=utf-8"];
    
    nsmutablestring *ip = [nsmutablestring stringwithcontentsofurl:ipurl encoding:nsutf8stringencoding Error:&error] ;
    Determine if the returned string is the data you want if
    ([IP hasprefix:@ ' var returncitysn =]) {
        //to handle the string, and then JSON parse
        //delete string extra string
        Nsrange range = nsmakerange (0);
        [IP Deletecharactersinrange:range];
        NSString * Nowip =[ip substringtoindex:ip.length-1];
        Converts a string into binary for JSON parsing
        nsdata * data = [Nowip datausingencoding:nsutf8stringencoding];
        Nsdictionary * dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil];
        NSLog (@ "%@", dict);
        return dict[@ "CIP"]? dict[@ "CIP": @ "";
    }
    return @ "";
}

The data that the Access interface takes:


Reference: http://blog.csdn.net/henry_moneybag/article/details/51463375

There is also an interface to get directly to the IP, but the return is slow, may return failure, not recommended.

    Nserror *error;
    Nsurl *ipurl = [Nsurl urlwithstring:@ "HTTP://IFCONFIG.ME/IP"];
    NSString *ip = [NSString stringwithcontentsofurl:ipurl encoding:nsutf8stringencoding error:&error];


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.