iOS獲取設備電量、內存、容量

一、獲取電池狀態code

-(CGFloat)getBatteryQuantity
 {
     return [[UIDevice currentDevice] batteryLevel];
 }

二、獲取總內存大小內存

-(long long)getTotalMemorySize
 {
     return [NSProcessInfo processInfo].physicalMemory;
 }

三、獲取當前可用內存get

-(long long)getAvailableMemorySize
 {
     vm_statistics_data_t vmStats;
     mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
     kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
     if (kernReturn != KERN_SUCCESS)
     {
         return NSNotFound;
     }

     return ((vm_page_size * vmStats.free_count + vm_page_size * vmStats.inactive_count));
 }

vm_statistics_data_t結構體中,free是空閒內存;active是已使用,但可被分頁的(在iOS中,只有在磁盤上靜態存在的才能被分頁,例如文件的內存映射,而動態分配的內存是不能被分頁的);inactive是不活躍的,也就是程序退出後卻沒釋放的內存,以便加快再次啓動,而當內存不足時,就會被回收,所以也可看做空閒內存;wire就是已使用,且不可被分頁的。it

四、獲取總磁盤容量分頁

-(long long)getTotalDiskSize
 {
     struct statfs buf;
     unsigned long long freeSpace = -1;
     if (statfs("/var", &buf) >= 0)
     {
         freeSpace = (unsigned long long)(buf.f_bsize * buf.f_blocks);
     }
     return freeSpace;
 }

五、獲取可用磁盤容量程序

-(long long)getAvailableDiskSize
 {
     struct statfs buf;
     unsigned long long freeSpace = -1;
     if (statfs("/var", &buf) >= 0)
     {
         freeSpace = (unsigned long long)(buf.f_bsize * buf.f_bavail);
     }
     return freeSpace;
 }
相關文章
相關標籤/搜索