IOS Get intranet and IP

Source: Internet
Author: User

Network IP directly access IP site can be https do not know what

#import "IPAddresses.h"

#import <sys/socket.h>

#import <sys/sockio.h>

#import <sys/ioctl.h>

#import <net/if.h>

#import <arpa/inet.h>

#import <ifaddrs.h>

#import <arpa/inet.h>

@implementation ipaddresses

+ (NSString *) getdeviceipaddresses//Intranet static IP

{

int SOCKFD = socket (af_inet,sock_dgram, 0);

if (sockfd <</span> 0) return nil; This error, due to reprint, do not understand, commented out no influence, understand the Great God welcome Guide

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, &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)->sin_addr )];

[IPs ADDOBJECT:IP];

}

}

Close (SOCKFD);

NSString *deviceip [email protected] "";

for (int i=0; i < Ips.count; i++) {

if (Ips.count >0) {

DEVICEIP = [NSString stringwithformat:@ "%@", Ips.lastobject];

}

}

return DEVICEIP;

}

Intranet IP

+ (NSString *) deviceipadress {

NSString *address = @ "mobile phone 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);

return address;

}

IOS Get intranet and IP

Related Article

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.