Get the native IP address

Source: Internet
Author: User

There are two ways to do this:

1 //get the native IP2-(NSString *) LocalIpAddress3 {4NSString *localip =Nil;5     structIfaddrs *Addrs;6     if(Getifaddrs (&addrs) = =0) {7         Const structIfaddrs *cursor =Addrs;8          while(Cursor! =NULL) {9             if(cursor->ifa_addr->sa_family = = Af_inet && (cursor->ifa_flags & iff_loopback) = =0)Ten             { One                 { ALocalip = [NSString Stringwithutf8string:inet_ntoa ((structsockaddr_in *) cursor->ifa_addr)sin_addr)]; -                      Break; -                 } the             } -cursor = cursor->Ifa_next; -         } - Freeifaddrs (Addrs); +     } -     returnLocalip; +}
1 //get the native IP address2-(NSString *) getipaddress3 {4NSString *address =@"Error";5     structIfaddrs *interfaces =NULL;6     structIfaddrs *temp_addr =NULL;7     intSuccess =0;8     9     //retrieve the current interfaces-returns 0 on successTenSuccess = Getifaddrs (&interfaces); One     if(Success = =0) { A         //Loop through linked list of interfaces -TEMP_ADDR =interfaces; -          while(Temp_addr! =NULL) { the             if(temp_addr->ifa_addr->sa_family = =af_inet) { -                 //Check If interface is En0 which are the WiFi connection on the IPhone -                 if([[NSString Stringwithutf8string:temp_addr->ifa_name] isequaltostring:@"En0"]) { -                     //Get nsstring from C String +address = [NSString Stringwithutf8string:inet_ntoa ((structsockaddr_in *) temp_addr->ifa_addr)sin_addr)]; -                 } +             } A              atTEMP_ADDR = temp_addr->Ifa_next; -         } -     } -      -     //Free Memory - Freeifaddrs (interfaces); in      -     returnaddress; to}

Get the native IP address

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.