ios 硬件設備獲取

以下內容從他處獲得,於大家分享
電池信息可以從UIDevice batteryLevel得到,但是隻能精確到0.05.
- (NSDictionary*)batteryLevel
{
CFTypeRef blob = IOPSCopyPowerSourcesInfo();
        CFArrayRef sources = IOPSCopyPowerSourcesList(blob);
        
        CFDictionaryRef pSource = NULL;
        const void *psValue;
        
        int numOfSources = CFArrayGetCount(sources);
        if (numOfSources == 0)
        {
                CFRelease(blob);
                CFRelease(sources);
                NSLog(@"qhk: Error in CFArrayGetCount");
                return nil;
        }
        
        for (int i = 0 ; i < numOfSources ; i++)
        {
                pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
                if (!pSource)
                {
                        CFRelease(blob);
                        CFRelease(sources);
                        NSLog(@"qhk: Error in IOPSGetPowerSourceDescription");
                        return nil;
                }
                
                psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));
                
                int curCapacity = 0;
                int maxCapacity = 0;
//                double percent;
                
                psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
                CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);
                
                psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
                CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);
                
//                percent = ((double)curCapacity/(double)maxCapacity * 100.0f);
                
                NSNumber* no1 = [NSNumber numberWithInt:curCapacity];
                NSNumber* no2= [NSNumber numberWithInt:maxCapacity];
                
                CFRelease(blob);
                CFRelease(sources);
                
                return [NSDictionary dictionaryWithObjectsAndKeys:no1, @"no1", no2, @"no2", nil];
                
//                return percent;
//                return (NSInteger)(percent + 0.5f);
        }
//#endif
        
        CFRelease(blob);
        CFRelease(sources);
        
        return nil;
}

這個可以精確到0.01,但是好像與系統顯示的仍有偏差。





得到平臺號:
- (NSString*) doDevicePlatform
{
    size_t size;
    int nR = sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = (char *)malloc(size);
    nR = sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
    free(machine);
    return platform;
}
在根據平臺號得到手機型號:
比如:

   if ([platform isEqualToString:@"iPhone1,1"])
    {
        return @"iPhone";
    }
    if ([platform isEqualToString:@"iPhone1,2"])
    {
        return @"iPhone3G";
    }
    if ([platform isEqualToString:@"iPhone2,1"])
    {
        return @"iPhone3GS";
    }
    if ([platform isEqualToString:@"iPhone3,1"])
    {
        return @"iPhone4";
    } 







得到mac地址:

- (void)printmacinfo
{
        bool success;
        struct ifaddrs *addrs;
        const struct ifaddrs *cursor;
        const struct sockaddr_dl *dlAddr;
        const uint8_t *base;
        
        success = getifaddrs(&addrs) == 0;
        if (success)
        {
                cursor = addrs;
                NSInteger idx = 0;
                while (cursor != NULL)
                {
                        ++idx;
                        NSString* macTitle = nil;
                        if ((cursor->ifa_flags & IFF_LOOPBACK) == 0 )
                        {
                                char* ifaname = (char *)cursor->ifa_name;
                                char* addr = inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr);
                                printf("%s ", ifaname);
                                printf("%s\n", addr);
//                                NSString* tmpstr1 = [NSString stringWithCString:ifaname encoding:NSUTF8StringEncoding];
//                                NSString* tmpstr2 = [NSString stringWithCString:addr encoding:NSUTF8StringEncoding];
//                                NSString *tmpStr = [NSString stringWithFormat:@"%@ %@", tmpstr1, tmpstr2];
                                
                                macTitle = [NSString stringWithFormat:@"%d %s %s", idx, ifaname, addr];
                                
                                [_arrKey addObject:macTitle];
                        }
                        if ( (cursor->ifa_addr->sa_family == AF_LINK)
                                && (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type ==IFT_ETHER)
                                )
                        {
                                dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
                                // fprintf(stderr, " sdl_nlen = %d\n", dlAddr->sdl_nlen);
                                // fprintf(stderr, " sdl_alen = %d\n", dlAddr->sdl_alen);
                                base = (const uint8_t *) &dlAddr->sdl_data[dlAddr->sdl_nlen];
                                printf(" MAC address ");
                                NSMutableString* tmpString = [[[NSMutableString alloc] initWithString:@"Mac:"] autorelease];
                                for (int i = 0; i < dlAddr->sdl_alen; i++)
                                {
                                        if (i != 0)
                                        {
                                                printf(":");
                                                [tmpString appendString:@":"];
                                        }
                                        printf("%02x", base);
                                        [tmpString appendFormat:@"%02X", base];
                                } 
                                printf("\n");
                                [_dic setObject:tmpString forKey:macTitle];
                        }
                        else if (macTitle != nil)
                        {
                                [_dic setObject:@"" forKey:macTitle];
                        }
                        cursor = cursor->ifa_next;
                }
        }
}







得到4種內存信息:

        mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
        vm_statistics_data_t vmstat;
        if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmstat, &count) != KERN_SUCCESS)
        {
                NSLog(@"Failed to get VM statistics.");
                [_dic setObject:@"Failed to get VM statistics." forKey:KTTMemorySize_Wire];
        }
        else
        {
                float total = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count;
                float wired = vmstat.wire_count / total * 100;
                float active = vmstat.active_count / total * 100;
                float inactive = vmstat.inactive_count / total * 100;
                float free = vmstat.free_count / total * 100;
//                NSString *str = [NSString stringWithFormat:@"%d %d %d %d %.2f %.2f %.2f %.2f %.0f %.0f"
//                                                 , vmstat.wire_count, vmstat.active_count, vmstat.inactive_count, vmstat.free_count
//                                                 , wired, active, inactive, free
//                                                 , total, total * pageSize
//                                                 ];

        }











cpu和總線頻率:
        int result;
        mib[0] = CTL_HW;
        mib[1] = HW_CPU_FREQ;
        length = sizeof(result);
        if (sysctl(mib, 2, &result, &length, NULL, 0) < 0)
        {
                perror("getting cpu frequency");
        }
        printf("CPU Frequency = %u hz\n", result);
        
        int result2;
        mib[0] = CTL_HW;
        mib[1] = HW_BUS_FREQ;
        length = sizeof(result2);
        if (sysctl(mib, 2, &result2, &length, NULL, 0) < 0)
        {
                perror("getting bus frequency");
        }
        printf("Bus Frequency = %u hz\n", result);







網絡方面使用的蘋果列子文檔中的Reachability.h和Reachability.m
外部ip訪問http://automation.whatismyip.com/n09230945.asp即可知道。
gethostbyname可知內部局域網ip。







 NetworkStatus netstatus = [reachable currentReachabilityStatus];
    switch (netstatus)
        {
                case NotReachable:
                        // 沒有網絡連接
                        reachableStatus = NSLocalizedString(@"No Network", "");
                        break;
                case ReachableViaWWAN:
                        // 使用3G網絡
                        reachableStatus = @"GPRS/3G";
                        break;
                case ReachableViaWiFi:
                        // 使用WiFi網絡
                        reachableStatus = @"WIFI";
                        break;
    }
這個可知網絡類型。







內存大小:

    size_t size = sizeof(int);
    int results;
    int mib[2] = {CTL_HW, HW_PHYSMEM};
    sysctl(mib, 2, &results, &size, NULL, 0);









總磁盤大小:
NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
[fattributes objectForKey:NSFileSystemSize];

剩餘空間:
[fattributes objectForKey:NSFileSystemFreeSize];









手機號碼:
這個也是undocument api
NSString* phoneNumber = CTSettingCopyMyPhoneNumber();











NSArray *getValue(NSString *iosearch)
{
    mach_port_t          masterPort;
    CFTypeID             propID = (CFTypeID) NULL;
    unsigned int         bufSize;
        
    kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
    if (kr != noErr) return nil;
        
    io_registry_entry_t entry = IORegistryGetRootEntry(masterPort);
    if (entry == MACH_PORT_NULL) return nil;
        
    CFTypeRef prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane, (CFStringRef) iosearch, nil, kIORegistryIterateRecursively);
    if (!prop) return nil;
        
        propID = CFGetTypeID(prop);
    if (!(propID == CFDataGetTypeID())) 
        {
                mach_port_deallocate(mach_task_self(), masterPort);
                CFRelease(prop);
                return nil;
        }
        
    CFDataRef propData = (CFDataRef) prop;
    if (!propData)
        {
                CFRelease(prop);
                return nil;
        }
        
    bufSize = CFDataGetLength(propData);
    if (!bufSize)
        {
                CFRelease(prop);
                return nil;
        }
        
    NSString *p1 = [[[NSString alloc] initWithBytes:CFDataGetBytePtr(propData) length:bufSize encoding:1] autorelease];
    mach_port_deallocate(mach_task_self(), masterPort);
        CFRelease(prop);
    return [p1 componentsSeparatedByString:@"\0"];
}
這個可以用來得到部分數據。



















- (NSString *) imei
{
        NSArray *results = getValue(@"device-imei");
        if (results) return [results objectAtIndex:0];
        return nil;
}

- (NSString *) serialnumber
{
        NSArray *results = getValue(@"serial-number");
        if (results) return [results objectAtIndex:0];
        return nil;
}

- (NSString *) backlightlevel
{
        NSArray *results = getValue(@"backlight-level");
        if (results) return [results objectAtIndex:0];
        return nil;
}

分別得到imei,序列號,背光
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章