引入头文件
#include <ifaddrs.h>
#include <arpa/inet.h>
- (NSString *)getIPAddress { NSString *address = @"error"; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; 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; } } freeifaddrs(interfaces); return address; }
iOS 获取手机IP
时间: 2024-10-10 17:37:08
iOS 获取手机IP的相关文章
IOS 获取手机ip地址
#include <ifaddrs.h> #include <arpa/inet.h> - (NSString *)getIPAddress { NSString *address = @"error"; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; // retrieve th
iOS 获取手机ip 《转/复制粘贴》
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #import <ifaddrs.h> #import <arpa/inet.h> // Get IP Address - (NSString *)getIPAddress { NSString *address = @"error"; struct ifaddrs *interfaces = N
ios获取局域网IP地址
#include <arpa/inet.h> #include <net/if.h> #include <ifaddrs.h> - (NSString *)localIPAddress { NSString *localIP = nil; struct ifaddrs *addrs; if (getifaddrs(&addrs)==0) { const struct ifaddrs *cursor = addrs; while (cursor != NULL)
转载-iOS获取设备IP地址
iOS获取设备IP地址 代码如下: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 #import <ifaddrs.h>#import <arpa/inet.h>#
iOS 获取手机当前所连接的网络的IP地址
1首先要在当前所在的类导入这几个头文件: #include <arpa/inet.h>#include <netdb.h>#include <net/if.h>#include <ifaddrs.h>#import <dlfcn.h> 2具体代码的实现: //获取手机的网络的ip地址- (NSString *)getIPAddress{ BOOL success; struct ifaddrs * addrs; const struct ifad
获取手机 IP
/** * 获取用户ip * * @return 获取用户ip */ + (NSString *)getIPAddress { NSString *address = @"an error occurred when obtaining ip address"; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; success = getifaddrs(&
iOS获取本地ip(基本通用)
今天有个朋友问我怎样訪问手机ip,上网找了几个,用了近200多行代码,最后发现头文件用的居然还是Linux中的,OC没有这个头文件.感觉socket本身应该能够后去自己的ip就试了一下,果然7.8行代码就攻克了.这个基本是c的socket,差点儿通用,别的平台也能够使用. - (NSString *)getLocalIP{ struct sockaddr_in sa; socklen_t len = sizeof(sa); if(getsockname(sockfd, (struct socka
iOS获取手机相关信息
iOS具体的设备型号: #include <sys/types.h> #include <sys/sysctl.h> - (void)test { //手机型号. size_t size; sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *machine = (char*)malloc(size); sysctlbyname("hw.machine", machine, &
ios 获取手机相关的信息
获取手机信息 应用程序的名称和版本号等信息都保存在mainBundle的一个字典中,用下面代码可以取出来 //获取版本号 NSDictionary *infoDict = [[NSBundle mainBundle]infoDictionary]; NSString *versionNum = [infoDict objectForKey:@"CFBundleVersion"];//版本号 NSString *appName = [infoDict objectForKey: