菜鸟教程小白 发表于 2022-12-11 19:40:08

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


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

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

<p>我得到以下值:</p>

<ul>
<li>NSFileSystemFreeSize:2287063040(~2.28 GB)</li>
<li>手机可用设置:5.77 GB</li>
<li>iTunes:5.67 GB 免费</li>
</ul>

<p>我使用的代码:</p>

<pre><code>+ (NSString*)getFreeDiskspace
{
NSDictionary *atDict = [ attributesOfFileSystemForPath:@&#34;/&#34; error:NULL];
unsigned freeSpace = [ unsignedIntValue];

return ;
}
</code></pre>

<p>其他信息:</p>

<ul>
<li>使用手机:iPhone 7</li>
<li>操作系统版本 11.2.1</li>
<li>手机已插入 macbook,因此我可以直接通过 xCode 构建</li>
</ul>

<p>谁能向我解释为什么 NSFileSystemFreeSize 与手机和 iTunes 不匹配?任何帮助表示赞赏,谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>有很多方法可以获得空闲空间</p>

<p>1、</p>

<pre><code>NSString *tempDir = ;
NSFileManager *fileManager = ;
NSDictionary&lt;NSFileAttributeKey, id&gt; *att = ;
NSNumber *freeSize = att;
const long long freeSpace = ;
</code></pre>

<p>2、仅iOS 11或更高版本</p>

<pre><code>NSURL *url = [ initFileURLWithPath:NSHomeDirectory()];
id AA = error:nil];
NSLog(@&#34;\nURLVolumeAvailableCapacityForImportantUsage : -----------\n%@&#34;, AA);
</code></pre>

<p>3、f_bavail或f_bfree供使用</p>

<pre><code>- (const long long) diskSpace {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
struct statfs tStats;
statfs([ cStringUsingEncoding:NSUTF8StringEncoding], &amp;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;
</code></pre>

<p>}</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - NSFileSystemFreeSize 返回不准确的可用磁盘空间?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48467776/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48467776/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - NSFileSystemFreeSize 返回不准确的可用磁盘空间?