如何获取 ios 设备的 存储容量

来源:互联网 发布:广州 java 三年 编辑:程序博客网 时间:2024/06/07 15:01

http://blog.csdn.net/a21064346/article/details/8270216


代码如下:

[csharp] view plaincopy
  1. #include <sys/statvfs.h>  
  2.   
  3. double get_disk_capacity ( char * path)  
  4. {  
  5.     struct statvfs sfs;  
  6.     unsigned long long result = 0;  
  7.     double disk_capacity = 0;  
  8.       
  9.     if ( statvfs ( path, &sfs) != -1 )  
  10.     {  
  11.         result = (unsigned long long)sfs.f_frsize * sfs.f_blocks;  
  12.           
  13.         if (result > 0)  
  14.         {  
  15.             disk_capacity = (double)result/(1024*1024);  
  16.         }  
  17.     }  
  18.       
  19.     return disk_capacity;  
  20. }  
  21.   
  22. - (void)printf {  
  23.     // Sum the size of the two logical partitions (root + user space)  
  24.     double total_capacity = get_disk_capacity("/") + get_disk_capacity("/private/var");  
  25.       
  26.     printf( "%.2f MB", total_capacity);  
  27. }  

方法2:

[csharp] view plaincopy
  1. NSDictionary *fsAttr = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];  
  2. float diskSize = [[fsAttr objectForKey:NSFileSystemSize] doubleValue] / 1000000000;  
  3. NSLog(@"Disk Size: %0.0f",diskSize);