結論としてはNSFileManagerのattributesOfFileSystemForPathを使用して値を取得する。ちなみに全体容量の値はiTunesで確認できる全体容量と同じものである。
<!–more–>
attributesOfFileSystemForPathはNSDictionaryに値を入れることになるのだが、その中にあるNSFileSystemFreeSizeは空き容量、NSFileSystemSizeは全体容量の値を持っている。
1
2
3
4
5
6
7
8
9
|
NSString *path = NSHomeDirectory();
NSNumber *allSize = nil;
NSNumber *freeSize = nil;
NSError *error = nil;
NSDictionary *dict = [[NSFileManager defaultManager]attributesOfFileSystemForPath:path error:&error];
allSize = [dict objectForKey:NSFileSystemSize]; // 使用容量
freeSize = [dict objectForKey:NSFileSystemFreeSize]; // 空き容量
NSLog(@"used:%.3f GB",[allSize floatValue]/1024/1024/1024);
NSLog(@"free:%.3f GB",[freeSize floatValue]/1024/1024/1024);
|
なお、全体容量と空き容量を取得するために、NSFileManagerのfileSystemAttributesAtPathを使用すると紹介している記事があるが、これは以前のiOSにおいて有効なものであり、現在は使えない。