gpt4 book ai didi

ios - NSFileSystemFreeSize 返回不准确的可用磁盘空间?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:04:59 44 4
gpt4 key购买 nike

所以我正在尝试查找我的 iPhone 上的可用磁盘空间量,并且我已经搜索了几篇较旧的帖子。这些帖子中的大多数都使用 NSFileManager 来检索 NSFileSystemFreeSize 值。

但是,与我在手机设置>常规>关于>可用中看到的数字相比,NSFileSystemFreeSize 中的数字似乎不准确。

我得到以下这些值:

  • NSFileSystemFreeSize:2287063040(~2.28 GB)
  • 手机的可用设置:5.77 GB
  • iTunes:5.67 GB 免费

我使用的代码:

+ (NSString*)getFreeDiskspace
{
NSDictionary *atDict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:@"/" error:NULL];
unsigned freeSpace = [[atDict objectForKey:NSFileSystemFreeSize] unsignedIntValue];

return [NSString stringWithFormat:@"%u", freeSpace];
}

其他信息:

  • 使用的手机:iPhone 7
  • 操作系统版本 11.2.1
  • 手机已插入 macbook,以便我可以直接通过 xCode 构建

谁能给我解释一下为什么 NSFileSystemFreeSize 与手机和 iTunes 不匹配?感谢任何帮助,谢谢!

最佳答案

有很多方法可以获得空闲空间

1、

NSString *tempDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary<NSFileAttributeKey, id> *att = [fileManager attributesOfFileSystemForPath:tempDir error:nil];
NSNumber *freeSize = att[NSFileSystemFreeSize];
const long long freeSpace = [freeSize longLongValue];

2、仅限iOS 11以上

NSURL *url = [[NSURL alloc] initFileURLWithPath:NSHomeDirectory()];
id AA = [url resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:nil][NSURLVolumeAvailableCapacityForImportantUsageKey];
NSLog(@"\nURLVolumeAvailableCapacityForImportantUsage : -----------\n%@", AA);

3、f_bavail或f_bfree供使用

- (const long long) diskSpace {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
struct statfs tStats;
statfs([[paths lastObject] cStringUsingEncoding:NSUTF8StringEncoding], &tStats);

// for total space
// const long long total_space = (long long)(tStats.f_blocks * tStats.f_bsize);
// return total_space;

// for freespace:
const long long free_space = (long long)(tStats. f_bavail * tStats.f_bsize);
return free_space;

关于ios - NSFileSystemFreeSize 返回不准确的可用磁盘空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48467776/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com