iOS 是否自动清理缓存目录?

Does iOS clean cache directory automatically?

我在这个路径保存媒体文件,我想知道 iOS 自动清理缓存还是我必须手动清理?

let documentsUrl = self.fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!

搜索了一下,没有具体的答案。

如果需要,操作系统可以清除此文件夹。

Documentation

Put data cache files in the Library/Caches/ directory. Cache data can be used for any data that needs to persist longer than temporary data, but not as long as a support file. Generally speaking, the application does not require cache data to operate properly, but it can use cache data to improve performance. Examples of cache data include (but are not limited to) database cache files and transient, downloadable content. Note that the system may delete the Caches/ directory to free up disk space, so your app must be able to re-create or download these files as needed. (emphasis mine)

是的,在某些情况下:

Use this directory to write any app-specific support files that your app can re-create easily. Your app is generally responsible for managing the contents of this directory and for adding and deleting files as needed.

In iOS 2.2 and later, the contents of this directory are not backed up by iTunes or iCloud. In addition, the system removes files in this directory during a full restoration of the device.

In iOS 5.0 and later, the system may delete the Caches directory on rare occasions when the system is very low on disk space. This will never occur while an app is running. However, be aware that restoring from backup is not necessarily the only condition under which the Caches directory can be erased.

您可以查看File System Programming Guide了解更多信息。