ios获取ip地址

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

进来接微信支付,后台要传ip地址,so~

首先要导这些头文件

1#include <stdio.h>2#include <stdlib.h>3#include <math.h>4#include <string.h>5#include <unistd.h>6#include <sys/ioctl.h>7#include <sys/types.h>8#include <sys/sysctl.h>9#include <sys/socket.h>10#include <netinet/in.h>11#include <netdb.h>12#include <arpa/inet.h>13#include <sys/sockio.h>14#include <net/if.h>15#include <net/ethernet.h>16#include <errno.h>17#include <net/if_dl.h>18#include <ifaddrs.h>19#include <mach/machine.h>

然后就是方法啦:

1+ (NSString *)getIPAddress2{3NSString *address =@"error";4structifaddrs *interfaces =NULL;5structifaddrs *temp_addr =NULL;6intsuccess =0;7//retrieve the current interfaces - returns 0 on success8success = getifaddrs(&interfaces);9if(success ==0)10{11//Loop through linked list of interfaces12temp_addr =interfaces;13while(temp_addr !=NULL)14{15if(temp_addr->ifa_addr->sa_family ==AF_INET)16{17//Check if interface is en0 which is the wifi connection on the iPhone18if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])19{20//Get NSString from C String21address = [NSString stringWithUTF8String:inet_ntoa(((structsockaddr_in *)temp_addr->ifa_addr)->sin_addr)];22}23}24temp_addr = temp_addr->ifa_next;25}26}27//Free memory28freeifaddrs(interfaces);29returnaddress;30}

我是给放在工具类里面了,哪里要用调哪里~so easy~

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航