本来很简单的一个东西,但是百度出来,各种答案,调研了爱奇艺,腾讯课堂,慕课网,网易云课堂App,发现同一部手机,居然计算出来的存储空间也是各不相同,相比较腾讯课堂,慕课网,网易云课堂App这三个误差较小.
直接上代码(总结别人方法)
- (float)getFreeDiskspace{
float totalSpace;
float totalFreeSpace=0.f;
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 floatValue];
totalFreeSpace = [freeFileSystemSizeInBytes floatValue];
}
return totalFreeSpace;
}
-(float)getTotalDiskSpaceInBytes {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
struct statfs tStats;
statfs([[paths lastObject] cString], &tStats);
float totalSpace = (float)(tStats.f_blocks * tStats.f_bsize);
return totalSpace;
}