显示ios设备信息的程序

学习ios开发一小段时间啦,分享个 个人练习的小作品,有越狱过的童鞋可以装上看看,由于用了2、3个undocument api,所以那个。。。。
高手可以无视啦。

以下是运行在本人iphone4上的截图,支持中文简体,中文繁体,英文,支持iphone和ipad,当然由于没有ipad,ipad的测试用的模拟器。
支持iphone4的Retina屏幕。
本来有6个标签,但是iphone的很多信息实在得不到,现在只剩下了4个标签。

IMG_0045.PNG
这里面的电量精确到0.01,用的undocument api,但是个人感觉总是比右上角系统自己显示的偏低3%以内。

IMG_0046.PNG
显示当前运行的进程,但是不知道如何得到进程的图标,因此统一用的图标。

IMG_0047.PNG
一些硬件信息,iphone4的A4 cpu频率实际上得不到,始终显示为0,只好根据手机型号判断,如果是iphone4则显示800Mhz,(据说是1G的cpu降频到800Mhz)。

IMG_0048.PNG
宣传devdiv的信息。

ipa包如下:
 FeiPhoneInfo.ipa

图标来源于网络。

每张图中右边的圆圈是ios5的手势触摸的东东。
展开来后是这个样子,可以有效的降低home键的使用。

IMG_0044.PNG

部分信息是UIDevice里的。
电池信息可以从UIDevice batteryLevel得到,但是只能精确到0.05.

  1. - (NSDictionary*)batteryLevel
  2. {
  3. CFTypeRef blob = IOPSCopyPowerSourcesInfo();
  4.         CFArrayRef sources = IOPSCopyPowerSourcesList(blob);
  5.         
  6.         CFDictionaryRef pSource = NULL;
  7.         const void *psValue;
  8.         
  9.         int numOfSources = CFArrayGetCount(sources);
  10.         if (numOfSources == 0)
  11.         {
  12.                 CFRelease(blob);
  13.                 CFRelease(sources);
  14.                 NSLog(@“qhk: Error in CFArrayGetCount”);
  15.                 return nil;
  16.         }
  17.         
  18.         for (int i = 0 ; i < numOfSources ; i++)
  19.         {
  20.                 pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
  21.                 if (!pSource)
  22.                 {
  23.                         CFRelease(blob);
  24.                         CFRelease(sources);
  25.                         NSLog(@“qhk: Error in IOPSGetPowerSourceDescription”);
  26.                         return nil;
  27.                 }
  28.                 
  29.                 psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));
  30.                 
  31.                 int curCapacity = 0;
  32.                 int maxCapacity = 0;
  33. //                double percent;
  34.                 
  35.                 psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
  36.                 CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, >curCapacity);
  37.                 
  38.                 psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
  39.                 CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, >maxCapacity);
  40.                 
  41. //                percent = ((double)curCapacity/(double)maxCapacity * 100.0f);
  42.                 
  43.                 NSNumber* no1 = [NSNumber numberWithInt:curCapacity];
  44.                 NSNumber* no2= [NSNumber numberWithInt:maxCapacity];
  45.                 
  46.                 CFRelease(blob);
  47.                 CFRelease(sources);
  48.                 
  49.                 return [NSDictionary dictionaryWithObjectsAndKeys:no1, @"no1", no2, @"no2", nil];
  50.                 
  51. //                return percent;
  52. //                return (NSInteger)(percent + 0.5f);
  53.         }
  54. //#endif
  55.         
  56.         CFRelease(blob);
  57.         CFRelease(sources);
  58.         
  59.         return nil;
  60. }
这个可以精确到0.01,但是好像与系统显示的仍有偏差。
得到平台号:
  1. - (NSString*) doDevicePlatform
  2. {
  3.     size_t size;
  4.     int nR = sysctlbyname(“hw.machine”, NULL, >size, NULL, 0);
  5.     char *machine = (char *)malloc(size);
  6.     nR = sysctlbyname(“hw.machine”, machine, >size, NULL, 0);
  7.     NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
  8.     free(machine);
  9.     return platform;
  10. }
在根据平台号得到手机型号:
比如:
  1. if ([platform isEqualToString:@"iPhone1,1"])
  2.     {
  3.         return @“iPhone”;
  4.     }
  5.     if ([platform isEqualToString:@"iPhone1,2"])
  6.     {
  7.         return @“iPhone3G”;
  8.     }
  9.     if ([platform isEqualToString:@"iPhone2,1"])
  10.     {
  11.         return @“iPhone3GS”;
  12.     }
  13.     if ([platform isEqualToString:@"iPhone3,1"])
  14.     {
  15.         return @“iPhone4″;
  16.     }
得到mac地址:
  1. - (void)printmacinfo
  2. {
  3.         bool success;
  4.         struct ifaddrs *addrs;
  5.         const struct ifaddrs *cursor;
  6.         const struct sockaddr_dl *dlAddr;
  7.         const uint8_t *base;
  8.         
  9.         success = getifaddrs(>addrs) == 0;
  10.         if (success)
  11.         {
  12.                 cursor = addrs;
  13.                 NSInteger idx = 0;
  14.                 while (cursor != NULL)
  15.                 {
  16.                         ++idx;
  17.                         NSString* macTitle = nil;
  18.                         if ((cursor->ifa_flags > IFF_LOOPBACK) == 0 )
  19.                         {
  20.                                 char* ifaname = (char *)cursor->ifa_name;
  21.                                 char* addr = inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr);
  22.                                 printf(“%s ”, ifaname);
  23.                                 printf(“%s\n”, addr);
  24. //                                NSString* tmpstr1 = [NSString stringWithCString:ifaname encoding:NSUTF8StringEncoding];
  25. //                                NSString* tmpstr2 = [NSString stringWithCString:addr encoding:NSUTF8StringEncoding];
  26. //                                NSString *tmpStr = [NSString stringWithFormat:@"%@ %@", tmpstr1, tmpstr2];
  27.                                 
  28.                                 macTitle = [NSString stringWithFormat:@"%d %s %s", idx, ifaname, addr];
  29.                                 
  30.                                 [_arrKey addObject:macTitle];
  31.                         }
  32.                         if ( (cursor->ifa_addr->sa_family == AF_LINK)
  33.                                 >> (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type ==IFT_ETHER)
  34.                                 )
  35.                         {
  36.                                 dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
  37.                                 // fprintf(stderr, ” sdl_nlen = %d\n”, dlAddr->sdl_nlen);
  38.                                 // fprintf(stderr, ” sdl_alen = %d\n”, dlAddr->sdl_alen);
  39.                                 base = (const uint8_t *) >dlAddr->sdl_data[dlAddr->sdl_nlen];
  40.                                 printf(“ MAC address ”);
  41.                                 NSMutableString* tmpString = [[[NSMutableString alloc] initWithString:@“Mac:”] autorelease];
  42.                                 for (int i = 0; i < dlAddr->sdl_alen; i++)
  43.                                 {
  44.                                         if (!= 0)
  45.                                         {
  46.                                                 printf(“:”);
  47.                                                 [tmpString appendString:@":"];
  48.                                         }
  49.                                         printf(“%02x”, base[i]);
  50.                                         [tmpString appendFormat:@"%02X", base[i]];
  51.                                 } 
  52.                                 printf(“\n”);
  53.                                 [_dic setObject:tmpString forKey:macTitle];
  54.                         }
  55.                         else if (macTitle != nil)
  56.                         {
  57.                                 [_dic setObject:@"" forKey:macTitle];
  58.                         }
  59.                         cursor = cursor->ifa_next;
  60.                 }
  61.         }
  62. }
页大小
  1. int pageSize = 0;
  2.         size_t length = sizeof(pageSize);
  3.         sysctlbyname(“hw.pagesize”, >pageSize, >length, NULL, 0);
得到4种内存信息:
  1.   mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
  2.         vm_statistics_data_t vmstat;
  3.         if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)>vmstat, >count) != KERN_SUCCESS)
  4.         {
  5.                 NSLog(@“Failed to get VM statistics.”);
  6.                 [_dic setObject:@"Failed to get VM statistics." forKey:KTTMemorySize_Wire];
  7.         }
  8.         else
  9.         {
  10.                 float total = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count;
  11.                 float wired = vmstat.wire_count / total * 100;
  12.                 float active = vmstat.active_count / total * 100;
  13.                 float inactive = vmstat.inactive_count / total * 100;
  14.                 float free = vmstat.free_count / total * 100;
  15. //                NSString *str = [NSString stringWithFormat:@"%d %d %d %d %.2f %.2f %.2f %.2f %.0f %.0f"
  16. //                                                 , vmstat.wire_count, vmstat.active_count, vmstat.inactive_count, vmstat.free_count
  17. //                                                 , wired, active, inactive, free
  18. //                                                 , total, total * pageSize
  19. //                                                 ];
  20.  
  21.         }
cpu和总线频率:
  1.        int result;
  2.         mib[0] = CTL_HW;
  3.         mib[1] = HW_CPU_FREQ;
  4.         length = sizeof(result);
  5.         if (sysctl(mib, 2, >result, >length, NULL, 0) < 0)
  6.         {
  7.                 perror(“getting cpu frequency”);
  8.         }
  9.         printf(“CPU Frequency = %u hz\n”, result);
  10.         
  11.         int result2;
  12.         mib[0] = CTL_HW;
  13.         mib[1] = HW_BUS_FREQ;
  14.         length = sizeof(result2);
  15.         if (sysctl(mib, 2, >result2, >length, NULL, 0) < 0)
  16.         {
  17.                 perror(“getting bus frequency”);
  18.         }
  19.         printf(“Bus Frequency = %u hz\n”, result);
网络方面使用的苹果列子文档中的Reachability.h和Reachability.m
外部ip访问http://automation.whatismyip.com/n09230945.asp即可知道。
gethostbyname可知内部局域网ip。
  1.    NetworkStatus netstatus = [reachable currentReachabilityStatus];
  2.     switch (netstatus)
  3.         {
  4.                 case NotReachable:
  5.                         // 没有网络连接
  6.                         reachableStatus = NSLocalizedString(@“No Network”, “”);
  7.                         break;
  8.                 case ReachableViaWWAN:
  9.                         // 使用3G网络
  10.                         reachableStatus = @“GPRS/3G”;
  11.                         break;
  12.                 case ReachableViaWiFi:
  13.                         // 使用WiFi网络
  14.                         reachableStatus = @“WIFI”;
  15.                         break;
  16.     }
这个可知网络类型。
内存大小:
  1. size_t size = sizeof(int);
  2.     int results;
  3.     int mib[2] = {CTL_HW, HW_PHYSMEM};
  4.     sysctl(mib, 2, >results, >size, NULL, 0);
总磁盘大小:
  1. NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
  2. [fattributes objectForKey:NSFileSystemSize];
剩余空间:
  1. [fattributes objectForKey:NSFileSystemFreeSize];
手机号码:
这个也是undocument api
  1. NSString* phoneNumber = CTSettingCopyMyPhoneNumber();
  1. NSArray *getValue(NSString *iosearch)
  2. {
  3.     mach_port_t          masterPort;
  4.     CFTypeID             propID = (CFTypeID) NULL;
  5.     unsigned int         bufSize;
  6.         
  7.     kern_return_t kr = IOMasterPort(MACH_PORT_NULL, >masterPort);
  8.     if (kr != noErr) return nil;
  9.         
  10.     io_registry_entry_t entry = IORegistryGetRootEntry(masterPort);
  11.     if (entry == MACH_PORT_NULL) return nil;
  12.         
  13.     CFTypeRef prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane, (CFStringRef) iosearch, nil, kIORegistryIterateRecursively);
  14.     if (!prop) return nil;
  15.         
  16.         propID = CFGetTypeID(prop);
  17.     if (!(propID == CFDataGetTypeID())) 
  18.         {
  19.                 mach_port_deallocate(mach_task_self(), masterPort);
  20.                 CFRelease(prop);
  21.                 return nil;
  22.         }
  23.         
  24.     CFDataRef propData = (CFDataRef) prop;
  25.     if (!propData)
  26.         {
  27.                 CFRelease(prop);
  28.                 return nil;
  29.         }
  30.         
  31.     bufSize = CFDataGetLength(propData);
  32.     if (!bufSize)
  33.         {
  34.                 CFRelease(prop);
  35.                 return nil;
  36.         }
  37.         
  38.     NSString *p1 = [[[NSString alloc] initWithBytes:CFDataGetBytePtr(propData) length:bufSize encoding:1] autorelease];
  39.     mach_port_deallocate(mach_task_self(), masterPort);
  40.         CFRelease(prop);
  41.     return [p1 componentsSeparatedByString:@"\0"];
  42. }
这个可以用来得到部分数据。
  1. - (NSString *) imei
  2. {
  3.         NSArray *results = getValue(@“device-imei”);
  4.         if (results) return [results objectAtIndex:0];
  5.         return nil;
  6. }
  7.  
  8. - (NSString *) serialnumber
  9. {
  10.         NSArray *results = getValue(@“serial-number”);
  11.         if (results) return [results objectAtIndex:0];
  12.         return nil;
  13. }
  14.  
  15. - (NSString *) backlightlevel
  16. {
  17.         NSArray *results = getValue(@“backlight-level”);
  18.         if (results) return [results objectAtIndex:0];
  19.         return nil;
  20. }
分别得到imei,序列号,背光。
 
 
posted @ 2012-09-07 08:12  careerman  阅读(2534)  评论(0编辑  收藏  举报