两种方法均与哔哩哔哩剩余内存统计比较过,数据一样,且都小于iOS系统 <设置>里统计的数据;
方法一:
#include <sys/param.h>
#include <sys/mount.h>
+ (NSString *)freeDiskSpaceInBytes{
struct statfs buf;
unsigned long long freeSpace = -1;
if (statfs("/var", &buf) >= 0) {
freeSpace = (unsigned long long)(buf.f_bsize * buf.f_bavail);
}
NSString *str = [NSString stringWithFormat:@"手机剩余存储空间为:%0.2lld MB",freeSpace/1024/1024];
return str;
}
方法二:
/// 总大小
float totalsize = 0.0;
/// 剩余大小
float freesize = 0.0;
/// 是否登录
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
if (dictionary)
{
NSNumber *_free = [dictionary objectForKey:NSFileSystemFreeSize];
freesize = [_free unsignedLongLongValue]*1.0/(1024);
NSNumber *_total = [dictionary objectForKey:NSFileSystemSize];
totalsize = [_total unsignedLongLongValue]*1.0/(1024);
} else
{
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
}
NSLog(@"totalsize = %.2f, freesize = %f",totalsize/1024/1024/1024, freesize/1024);