项目中的部分代码,把贴出来吧:
-(long long) freeDiskSpace{
struct statfs buf;
long long freespace = -1;
if(statfs("/var", &buf) >= 0){
// freespace = (long long)(buf.f_bsize * buf.f_bfree); //剩余空间
freespace = (long long)(buf.f_bsize * buf.f_blocks); //总空间(自己找的,这个不是很确定)
}
return freespace;
}
NSString * formattedFileSize(unsigned long long size)
{
NSString *formattedStr = nil;
if (size == 0)
formattedStr = @"0.0MB";
else
if (size > 0 && size < 1024)
formattedStr = [NSString stringWithFormat:@"%qu bytes", size];
else if (size >= 1024 && size < pow(1024, 2))
formattedStr = [NSString stringWithFormat:@"%.1f KB", (size / 1024.)];
else if (size >= pow(1024, 2) && size < pow(1024, 3))
formattedStr = [NSString stringWithFormat:@"%.2f MB", (size / pow(1024, 2))];
else if (size >= pow(1024, 3))
formattedStr = [NSString stringWithFormat:@"%.3f GB", (size / pow(1024, 3))];
return formattedStr;
}