簡體   English   中英

在 iphone 上檢測可用磁盤空間時得到錯誤的值

[英]Got wrong value while detecting available disk space on iphone

我關注http://kdbdallas.com/2008/12/27/maciphone-show-availble-useable-diskspace/如何找出 iphone 磁盤空間? 檢測可用磁盤空間。 但是,似乎結果不正確

我在 iphone 4 ios 4.2.1 上測試過。

在Setting中,Capacity是29.1G Availability是24.2G

我想獲得 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的文件系統進行了更多的搜索之后,我發現iphone有2個分區:一個用於操作系統,另一個用於用戶數據。 我得到的值是操作系統分區的屬性。 所以我將路徑更改為“private/var”,這是數據分區的路徑。 結果,我得到了正確的可用磁盤空間值。

試試這個代碼

-(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 為單位的總可用空間

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM