ios - 如何在 iOS 11 上检测 iPhone/iPad 设备上的总可用/空闲磁盘空间

标签 ios objective-c iphone ipad ios11

在 iOS 11 中,我无法从字典键 NSFileSystemFreeSize 获取设备的正确可用大小(磁盘空间)。而不是提供 34.4 GB,而是提供 4 GB 的可用空间。

下面是我使用的代码

pragma mark - 格式化程序

- (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;
}

最佳答案

Objective-C (已转换)

- (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;
}

关于ios - 如何在 iOS 11 上检测 iPhone/iPad 设备上的总可用/空闲磁盘空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48516836/

相关文章:

iphone - EAGLView 到 UIImage 颜色转换问题

ios - 用动画替换segue?

ios - 在 Objective-C 中禁用 CCButton

objective-c - 无论如何让(包装)NSTextField 在按下回车键时写一个回车?

ios - 在表 IOS 中选择单元格

iphone - OSStatus 错误 -50?

objective-c - 如何将一个 NSMutablearray 复制到另一个 NSMutablearray?

iphone - 如何获取联系人中选定人员的电话号码

iphone - ios中Json对象到Sqlite数据库

ios - 错误域=kAFAssistantErrorDomain 代码=209 "(null)"