iOS获取手机ip地址代码
程序员文章站
2024-02-15 10:21:28
本文实例为大家分享了ios获取手机ip地址的具体代码,供大家参考,具体内容如下
#import
#import
本文实例为大家分享了ios获取手机ip地址的具体代码,供大家参考,具体内容如下
#import <ifaddrs.h> #import <arpa/inet.h> // get ip address - (nsstring *)getipaddress { nsstring *address = @"error"; struct ifaddrs *interfaces = null; struct ifaddrs *temp_addr = null; int success = 0; // retrieve the current interfaces - returns 0 on success success = getifaddrs(&interfaces); if (success == 0) { // loop through linked list of interfaces temp_addr = interfaces; while(temp_addr != null) { if(temp_addr->ifa_addr->sa_family == af_inet) { // check if interface is en0 which is 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(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; } } temp_addr = temp_addr->ifa_next; } } // free memory freeifaddrs(interfaces); return address; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。