iOS 计算磁盘大小的方法

发现网上计算磁盘大小都不准确,所以自己总结了一下,与腾讯手机管家的对比过,数值一样

  • iOS11才出的方法,只适合 iOS11
  • 在 iOS11 中 1G == 1000M 而不是1024M, iOS11 之前是 1024
if (@available(iOS 11.0, *)) {
        NSDictionary *results = [fileURL resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:&error];
        if (!results) {
            NSLog(@"Error retrieving resource keys: %@%@",[error localizedDescription], [error userInfo]);
            abort();
            
        }
        NSLog(@"Available capacity for important usage: %@",results);
    } else {
        // Fallback on earlier versions
    }
  • 自己写的方法
// 空余硬盘大小
+ (CGFloat)freeDiskSpaceInBytes{
    
    CGFloat totalFreeSpace = 0.0;
    NSError *error = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
    
    if (dictionary) {
        NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
        totalFreeSpace = [freeFileSystemSizeInBytes floatValue];
        NSLog(@" %f GB Free memory available.", ((totalFreeSpace/1024.f)/1024.f)/1024.f);
    } else {
        NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);
    }
    
    if (@available(iOS 11.0, *)) {
        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:NSTemporaryDirectory()];
        NSDictionary *results = [fileURL resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:&error];
        if (!results) {
            NSLog(@"Error retrieving resource keys: %@%@",[error localizedDescription], [error userInfo]);
            abort();
            
        }
        NSLog(@"Available capacity for important usage: %lf",[[results objectForKey:NSURLVolumeAvailableCapacityForImportantUsageKey] floatValue]);
        totalFreeSpace = [[results objectForKey:NSURLVolumeAvailableCapacityForImportantUsageKey] floatValue];
    } else {
        
    }
    return totalFreeSpace;
   
}
// 总共硬盘大小
+ (CGFloat)totalDiskSpace{
    
    CGFloat totalSpace = 0.0;
    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];
        totalSpace = [fileSystemSizeInBytes floatValue];
        NSLog(@"Memory Capacity of %f GB .", ((totalSpace/1024.f)/1024.f/1024.f));
    } else {
        NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);
    }
    return totalSpace;
}

  • 转换
+(NSString *)fileSizeToString:(CGFloat)fileSize
{
       NSInteger KB;
    if (iOS11Later) {
        KB = 1000.f;
    }else{
        KB = 1024.f;
    }
    
       NSInteger MB = KB*KB;
       NSInteger GB = MB*KB;
    
       if (fileSize < 10.f)  {
               return @"0 B";
            }else if (fileSize < KB)    {
                    return @"< 1 KB";
                }else if (fileSize < MB)    {
                        return [NSString stringWithFormat:@"%.1f KB",((float)fileSize)/KB];
                    }else if (fileSize < GB)    {
                            return [NSString stringWithFormat:@"%.1f MB",((float)fileSize)/MB];
                        }else   {
                                 return [NSString stringWithFormat:@"%.1f GB",((float)fileSize)/GB];
                            }
}

推荐阅读更多精彩内容