擷取手機IP位址的方法:
然後寫一個方法如下:
//擷取手機ip位址
+(NSString *)getiPhoneIPAddresses{
int sockfd =socket(AF_INET,SOCK_DGRAM, 0);
NSMutableArray *ips = [NSMutableArray array];
int BUFFERSIZE =4096;
struct ifconf ifc;
char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
struct ifreq *ifr, ifrcopy;
ifc.ifc_len = BUFFERSIZE;
ifc.ifc_buf = buffer;
if (ioctl(sockfd,SIOCGIFCONF, &ifc) >= 0){
for (ptr = buffer; ptr < buffer + ifc.ifc_len; ){
ifr = (struct ifreq *)ptr;
int len =sizeof(struct sockaddr);
if (ifr->ifr_addr.sa_len > len) {
len = ifr->ifr_addr.sa_len;
}
ptr += sizeof(ifr->ifr_name) + len;
if (ifr->ifr_addr.sa_family !=AF_INET) continue;
if ((cptr = (char *)strchr(ifr->ifr_name,':')) != NULL) *cptr =0;
if (strncmp(lastname, ifr->ifr_name,IFNAMSIZ) == 0)continue;
memcpy(lastname, ifr->ifr_name,IFNAMSIZ);
ifrcopy = *ifr;
ioctl(sockfd,SIOCGIFFLAGS, &ifrcopy);
if ((ifrcopy.ifr_flags &IFF_UP) == 0)continue;
NSString *ip = [NSString stringWithFormat:@"%s",inet_ntoa(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr)];
[ips addObject:ip];
}
}
close(sockfd);
NSString *deviceIP =@"";
for (int i=0; i < ips.count; i++){
if (ips.count >0){
deviceIP = [NSString stringWithFormat:@"%@",ips.lastObject];
}
}
return deviceIP;
}
- //手機序列号
- NSString* identifierNumber = [[UIDevice currentDevice] uniqueIdentifier];
- NSLog(@"手機序列号: %@",identifierNumber);
- //手機别名: 使用者定義的名稱
- NSString* userPhoneName = [[UIDevice currentDevice] name];
- NSLog(@"手機别名: %@", userPhoneName);
- //裝置名稱
- NSString* deviceName = [[UIDevice currentDevice] systemName];
- NSLog(@"裝置名稱: %@",deviceName );
- //手機系統版本
- NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
- NSLog(@"手機系統版本: %@", phoneVersion);
- //手機型号
- NSString* phoneModel = [[UIDevice currentDevice] model];
- NSLog(@"手機型号: %@",phoneModel );
- //地方型号 (國際化區域名稱)
- NSString* localPhoneModel = [[UIDevice currentDevice] localizedModel];
- NSLog(@"國際化區域名稱: %@",localPhoneModel );
- NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
- // 目前應用名稱
- NSString *appCurName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
- NSLog(@"目前應用名稱:%@",appCurName);
- // 目前應用軟體版本 比如:1.0.1
- NSString *appCurVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
- NSLog(@"目前應用軟體版本:%@",appCurVersion);
- // 目前應用版本号碼 int類型
- NSString *appCurVersionNum = [infoDictionary objectForKey:@"CFBundleVersion"];
- NSLog(@"目前應用版本号碼:%@",appCurVersionNum);
結果:
[plain] view plain copy
- 2012-10-19 14:07:47.622 myDemo[5779:707] 手機序列号: 6685c75e34104be0b04c6ceb72985dc381f0f746
- 2012-10-19 14:07:47.624 myDemo[5779:707] 手機别名: “spring sky”的 iPod
- 2012-10-19 14:07:47.627 myDemo[5779:707] 裝置名稱: iPhone OS
- 2012-10-19 14:07:47.629 myDemo[5779:707] 手機系統版本: 5.1.1
- 2012-10-19 14:07:47.641 myDemo[5779:707] 手機型号: iPod touch
- 2012-10-19 14:07:47.642 myDemo[5779:707] 國際化區域名稱: iPod touch
- 2012-10-19 14:07:47.643 myDemo[5779:707] 目前應用名稱:myDemo
- 2012-10-19 14:07:47.645 myDemo[5779:707] 目前應用軟體版本:1.0.1