DiskFreeSpaceManager.m 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // DiskFreeSpaceManager.m
  3. // StudentDaya
  4. //
  5. // Created by Kyle on 2021/10/9.
  6. // Copyright © 2021 DayaMusic. All rights reserved.
  7. //
  8. #import "DiskFreeSpaceManager.h"
  9. @implementation DiskFreeSpaceManager
  10. + (CGFloat)QueryDiskSpaceFree {
  11. NSURL *fileUrl = [NSURL fileURLWithPath:NSHomeDirectory()];
  12. int64_t space = 0;
  13. if (@available(iOS 11.0, *)) {
  14. NSDictionary *spaceDict = [fileUrl resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:nil];
  15. space = [spaceDict[NSURLVolumeAvailableCapacityForImportantUsageKey] longLongValue];
  16. } else {
  17. NSError *error = nil;
  18. NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
  19. if (error) {
  20. return -1;
  21. }
  22. space = [[attrs objectForKey:NSFileSystemFreeSize] longLongValue];
  23. if (space < 0) {
  24. space = -1;
  25. }
  26. }
  27. CGFloat freeSize = space * 1.0 / 1024.0 / 1024.0 / 1024.0;
  28. NSLog(@"free size %f", freeSize);
  29. return freeSize;
  30. }
  31. @end