【问题标题】:Got wrong value while detecting available disk space on iphone检测 iphone 上的可用磁盘空间时得到错误值
【发布时间】:2012-04-06 09:49:43
【问题描述】:

我关注http://kdbdallas.com/2008/12/27/maciphone-show-availble-useable-diskspace/how do i find out iphone Disk Space? 检测可用的磁盘空间。但是,似乎结果不正确

我在 iphone 4 ios 4.2.1 上测试过。

在设置中, 容量为 29.1 G 可用性为 24.2 G

我想获得 24.2 G 的值。所以我按照上面提到的教程进行操作。

int main(int argc, char *argv[]) {
        .....

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
        NSLog(@"path %@", paths);

        struct statfs tStats;
        statfs([[paths lastObject] UTF8String], &tStats);

        NSString *sizeType;

        float total_space = (float)(tStats.f_blocks * tStats.f_bsize);  
        NSLog(@"total space: %f", total_space);
        NSLog(@"total space: %@", convertUnit(total_space));

        float free_space = (float)(tStats.f_bfree * tStats.f_bsize);
        NSLog(@"free space: %f", free_space);
        NSLog(@"free space: %@", convertUnit(free_space));

        float free_space2 = (float)(tStats.f_bavail * tStats.f_bsize);      
        NSLog(@"free blocks avail to non-superuser: %f", free_space2);
        NSLog(@"free blocks avail to non-superuser: %@", convertUnit(free_space2));

        uint32_t block_size = (tStats.f_bsize);     
        NSLog(@"block size: %d", block_size);
        NSLog(@"block size: %@", convertUnit(block_size));
        .....
}       

NSString* convertUnit (float value) {
    NSString *sizeType;
    if (value > 1024)
    {
        //Kilobytes
        value = value / 1024;

        sizeType = @" KB";
    }

    if (value > 1024)
    {
        //Megabytes
        value = value / 1024;

        sizeType = @" MB";
    }

    if (value > 1024)
    {
        //Gigabytes
        value = value / 1024;

        sizeType = @" GB";
    }

    return [[@"Available Disk Space: " stringByAppendingFormat:@"%.2f", value] stringByAppendingString:sizeType];

}

结果:

total space: 17363854862722269184.000000
total space: Available Disk Space: 16171350016.00 GB

free space: 17743592094095638528.000000
free space: Available Disk Space: 16525007872.00 GB

free blocks avail to non-superuser: 17432956969504735232.00000
free blocks avail to non-superuser: Available Disk Space: 16235706368.00 GB

block size: 803203484
block size: Available Disk Space: 765.99 MB

【问题讨论】:

    标签: iphone objective-c disk


    【解决方案1】:

    在对iphone的文件系统进行了更多搜索后,我发现iphone有2个分区:一个用于操作系统,另一个用于用户数据。我得到的值是操作系统分区的属性。所以我将路径更改为“private/var”,这是数据分区的路径。结果,我得到了正确的可用磁盘空间值。

    【讨论】:

      【解决方案2】:

      试试这个代码

      -(uint64_t)getFreeDiskspace {
      
          uint64_t totalSpace = 0;
          uint64_t totalFreeSpace = 0;
          NSError *error = nil;  
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
          NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];  
      
          if (dictionary) {  
              NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];  
              NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
              totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
              totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
              NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
          } else {  
              NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);  
          }  
      
          return ((totalFreeSpace/1024ll)/1024ll);
      }
      

      这将为您提供以 MB 为单位的总可用空间

      【讨论】:

        猜你喜欢
        • 2012-01-23
        • 1970-01-01
        • 2011-08-08
        • 2018-07-09
        • 2012-11-15
        • 1970-01-01
        • 1970-01-01
        • 2014-08-06
        • 2010-09-27
        相关资源
        最近更新 更多