美文网首页
iOS 获取可用手机容量

iOS 获取可用手机容量

作者: 微笑了 | 来源:发表于2022-09-21 11:07 被阅读0次
        
    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<NSURLResourceKey, id> * dict = [url resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:&error];
        if (error) {
            return 0;
        }
        long long space = [dict[NSURLVolumeAvailableCapacityForImportantUsageKey] longLongValue];
        return space;
    } else {
        NSError * error = nil;
        NSDictionary<NSFileAttributeKey, id> * 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 获取可用手机容量

      本文链接:https://www.haomeiwen.com/subject/ieswortx.html