iOS 获取可用手机容量

        
    NSString* free = [self freeDiskSpaceInGB];
    NSLog(@"free:%@ GB",free);

 


- (long)freeDiskSpaceInBytes {
    if (@available(iOS 11.0, *)) {
        [NSURL alloc];
        NSURL * url = [[NSURL alloc]initFileURLWithPath:[NSString stringWithFormat:@"%@",NSHomeDirectory()]];
        NSError * error = nil;
        NSDictionary * dict = [url resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:&error];
        if (error) {
            return 0;
        }
        long long space = [dict[NSURLVolumeAvailableCapacityForImportantUsageKey] longLongValue];
        return space;
    } else {
        NSError * error = nil;
        NSDictionary * systemAttributes =  [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
        if (error) {
            return 0;
        }
        long long space = [systemAttributes[NSFileSystemFreeSize] longLongValue];
        return space;
    }
}

- (NSString *)freeDiskSpaceInGB
{
    return [NSByteCountFormatter stringFromByteCount:[self freeDiskSpaceInBytes] countStyle:NSByteCountFormatterCountStyleDecimal];
}```

你可能感兴趣的:(iOS 获取可用手机容量)