// iOS11 之前使用该方法获取
- (long)getTotalDiskSpace{
long totalsize = -1;
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];
} else
{
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
}
return totalsize;
}
// iOS11 之后使用该方法获取
- (void)getTotalDiskSpace{
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:NSTemporaryDirectory()];
NSDictionary *results = [fileURL resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:nil];
NSLog(@"剩余可用空间:%@",results[NSURLVolumeAvailableCapacityForImportantUsageKey]);
}
//iOS11 之前使用该方法获取
-(long)getFreeDiskSpace{
long freesize = -1;
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];
} else
{
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
}
return freesize;
}
// iOS11 之后使用该方法获取
- (void)getTotalDiskSpace{
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:NSTemporaryDirectory()];
NSDictionary *results = [fileURL resourceValuesForKeys:@[NSURLVolumeTotalCapacityKey] error:nil];
NSLog(@"剩余可用空间:%@",results[NSURLVolumeTotalCapacityKey]);
}