- 获取设备总的存储空间:
- (float)getTotalDiskSpace{
float totalsize = 0.0;
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
if (dictionary)
{
NSNumber *_total = [dictionary objectForKey:NSFileSystemSize];
totalsize = [_total unsignedLongLongValue]*1.0;
} else
{
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
}
return totalsize;
}
- 获取设备可用存储空间:
-(float)getFreeDiskSpace{
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;
} else
{
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
}
return freesize;
}
调用:
float total = [self getTotalDiskSpace];
NSLog(@"total:%f GB",total/(1024*1024*1024));
float free = [self getFreeDiskSpace];
NSLog(@"free:%f GB",free/(1024*1024*1024));
网友评论