2012-04-06 9 views
0

私はhttp://kdbdallas.com/2008/12/27/maciphone-show-availble-useable-diskspace/ とを使用して、使用可能なディスク容量を検出しています。しかし、結果が正しくないと思われるiphoneで使用可能なディスク容量を検出している間に間違った値を取得しました

私は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 

答えて

0

iphoneのファイルシステムについての詳細検索を実行した後、私はiphoneは2つのパーティションがあることが判明:OS用とユーザデータの他に。私が得た値は、OSパーティションの属性です。そこで私は、パスを「private/var」に変更しました。これはデータパーティションのパスです。その結果、私は空きディスク容量に正しい値を得ました。

0

これはあなたMB

の総空きスペースを与える

-(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); 
} 

このコードを試してみてください

関連する問題