进来接微信支付,后台要传ip地址,so~
首先要导这些头文件
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 #include 19 #include
然后就是方法啦:
1 + (NSString *)getIPAddress 2 { 3 NSString *address = @"error"; 4 struct ifaddrs *interfaces = NULL; 5 struct ifaddrs *temp_addr = NULL; 6 int success = 0; 7 // retrieve the current interfaces - returns 0 on success 8 success = getifaddrs(&interfaces); 9 if (success == 0)10 {11 // Loop through linked list of interfaces12 temp_addr = interfaces;13 while(temp_addr != NULL)14 {15 if(temp_addr->ifa_addr->sa_family == AF_INET)16 {17 // Check if interface is en0 which is the wifi connection on the iPhone18 if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])19 {20 // Get NSString from C String21 address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];22 }23 }24 temp_addr = temp_addr->ifa_next;25 }26 }27 // Free memory28 freeifaddrs(interfaces);29 return address;30 }
我是给放在工具类里面了,哪里要用调哪里~so easy~