How to detect total available/free disk space on the iPhone/iPad device?

后端 未结 18 1760
别跟我提以往
别跟我提以往 2020-11-22 11:14

I\'m looking for a better way to detect available/free disk space on the iPhone/iPad device programmatically.
Currently I\'m using the NSFileManager to detect the disk s

相关标签:
18条回答
  • 2020-11-22 11:47

    Revised source using unsigned long long:

    - (uint64_t)freeDiskspace
    {
        uint64_t totalSpace = 0;
        uint64_t totalFreeSpace = 0;
    
        __autoreleasing 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 unsignedLongLongValue];
            totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
            NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
        } else {  
            NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %d", [error domain], [error code]);  
        }  
    
        return totalFreeSpace;
    }
    

    EDIT: it seems someone edited this code to use 'uint64_t' instead of 'unsigned long long'. While in the foreseeable future this should be just fine, they are not the same. 'uint64_t' is 64 bits and will always be that. In 10 years 'unsigned long long' might be 128. its a small point but why I used unsignedLongLong.

    0 讨论(0)
  • 2020-11-22 11:48

    I know this post is a bit old, but I think this answer can help someone. If you want to know the used/free/total disk space on the device you can use Luminous. It's written in Swift. You have only to call :

    Luminous.System.Disk.freeSpace()
    Luminous.System.Disk.usedSpace()
    

    or

    Luminous.System.Disk.freeSpaceInBytes()
    Luminous.System.Disk.usedSpaceInBytes()
    
    0 讨论(0)
  • 2020-11-22 11:52

    If you need formatted string with size you can take a look at nice library on GitHub:

    #define MB (1024*1024)
    #define GB (MB*1024)
    
    @implementation ALDisk
    
    #pragma mark - Formatter
    
    + (NSString *)memoryFormatter:(long long)diskSpace {
        NSString *formatted;
        double bytes = 1.0 * diskSpace;
        double megabytes = bytes / MB;
        double gigabytes = bytes / GB;
        if (gigabytes >= 1.0)
            formatted = [NSString stringWithFormat:@"%.2f GB", gigabytes];
        else if (megabytes >= 1.0)
            formatted = [NSString stringWithFormat:@"%.2f MB", megabytes];
        else
            formatted = [NSString stringWithFormat:@"%.2f bytes", bytes];
    
        return formatted;
    }
    
    #pragma mark - Methods
    
    + (NSString *)totalDiskSpace {
        long long space = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemSize] longLongValue];
        return [self memoryFormatter:space];
    }
    
    + (NSString *)freeDiskSpace {
        long long freeSpace = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemFreeSize] longLongValue];
        return [self memoryFormatter:freeSpace];
    }
    
    + (NSString *)usedDiskSpace {
        return [self memoryFormatter:[self usedDiskSpaceInBytes]];
    }
    
    + (CGFloat)totalDiskSpaceInBytes {
        long long space = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemSize] longLongValue];
        return space;
    }
    
    + (CGFloat)freeDiskSpaceInBytes {
        long long freeSpace = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemFreeSize] longLongValue];
        return freeSpace;
    }
    
    + (CGFloat)usedDiskSpaceInBytes {
        long long usedSpace = [self totalDiskSpaceInBytes] - [self freeDiskSpaceInBytes];
        return usedSpace;
    }
    
    0 讨论(0)
  • 2020-11-22 11:52

    Following code is Swift 3.0 version implementation of the answer previously provided by ChrisJF:

    func freeSpaceInBytes() -> NSString {
    
        var remainingSpace = NSLocalizedString("Unknown", comment: "The remaining free disk space on this device is unknown.")
    
        do {
            let dictionary =  try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory())
            let freeSpaceSize = ((dictionary[FileAttributeKey.systemFreeSize] as AnyObject).longLongValue)!
            remainingSpace = ByteCountFormatter.string(fromByteCount: freeSpaceSize, countStyle: ByteCountFormatter.CountStyle.file)
        }
        catch let error {
            NSLog(error.localizedDescription)
        }
    
        return remainingSpace as NSString
    
    }
    
    0 讨论(0)
  • 2020-11-22 11:53

    Important clarification (at least for me). If I connect my iPod to my Mac this is the info showed by iTunes App.

    iPod memory informations from iTunes app

    When I use the above code:

    long long freeSpace = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil]
                                objectForKey:NSFileSystemFreeSize] longLongValue];
    
    NSString *free1 = [NSByteCountFormatter stringFromByteCount:freeSpace countStyle:NSByteCountFormatterCountStyleFile];
    
    [label1 setText:free1];
    
    NSString *free2 = [NSByteCountFormatter stringFromByteCount:freeSpace countStyle:NSByteCountFormatterCountStyleBinary];
    
    [label2 setText:free2];
    

    The countStyle NSByteCountFormatterCountStyleFile show me: 17,41 GB

    The countStyle NSByteCountFormatterCountStyleBinary show me: 16,22 GB

    16,22 GB (NSByteCountFormatterCountStyleBinary) It is EXACTLY the number that iTunes App show me when I connect my iPod to my Mac.

    0 讨论(0)
  • 2020-11-22 11:58

    Update with a new accurate API to get available size on disk available in iOS11. Here is the description for the new API resource key:

    #if os(OSX) || os(iOS)
    /// Total available capacity in bytes for "Important" resources, including space expected to be cleared by purging non-essential and cached resources. "Important" means something that the user or application clearly expects to be present on the local system, but is ultimately replaceable. This would include items that the user has explicitly requested via the UI, and resources that an application requires in order to provide functionality.
    /// Examples: A video that the user has explicitly requested to watch but has not yet finished watching or an audio file that the user has requested to download.
    /// This value should not be used in determining if there is room for an irreplaceable resource. In the case of irreplaceable resources, always attempt to save the resource regardless of available capacity and handle failure as gracefully as possible.
    @available(OSX 10.13, iOS 11.0, *) @available(tvOS, unavailable) @available(watchOS, unavailable)
    public var volumeAvailableCapacityFor Usage: Int64? { return _get(.volumeAvailableCapacityForImportantUsageKey) }
    #endif
    

    I cross compared the results from key "FileAttributeKey.systemFreeSize" and key "URLResourceKey.volumeAvailableCapacityForImportantUsageKey" and found the results returned form "volumeAvailableCapacityForImportantUsageKey" exactly matches the available storage shown on UI. Here is the swift implementation:

    class var freeDiskSpaceInBytesImportant:Int64 {
        get {
            do {
                return try URL(fileURLWithPath: NSHomeDirectory() as String).resourceValues(forKeys: [URLResourceKey.volumeAvailableCapacityForImportantUsageKey]).volumeAvailableCapacityForImportantUsage!
            } catch {
                return 0
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题