获取 ios 内部存储内存信息 xamarin forms ios

Get ios internal storage memory information xamarin forms ios

我无法从 iPhone 设备获得实际的免费存储空间 space。我正在使用 this link 以 xamarin 形式 ios 获取存储空间 space。以下是 link.

中的代码
public double GetRemainingInternalMemoryStorage()
    {
        NSFileSystemAttributes applicationFolder = NSFileManager.DefaultManager.GetFileSystemAttributes(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
        var freeSpace = applicationFolder.FreeSize;
        var totalSpace = applicationFolder.Size;

        
        Console.WriteLine("totalSpace " + freeSpace);
        
        return freeSpace;
    }

我正在开发一项功能,如果存储 space 小于阈值,我需要向用户发送警报。我没有获得准确的存储空间 space,所以我的功能无法正常工作。

我的设备总共有 32 GB 的存储内存,但是当我检查上面的代码时,它看到了 31989469184 字节,接近 31.98 GB (31989469184/1000/1000/1000),这看起来接近 correct.But 类似设备的免费 space 是 14.2 GB,使用上面的代码它看到 12259602432 字节,接近 12.25 GB。我不确定为什么少了 2 GB。

以上 linked android 代码运行良好。如何在 iOS 中计算准确的免费 space?

如上所述,iOS 在缓存中保留一些内存,我填满了设备的全部内存,以便设备中只剩下 300 MB 空闲 space。那时我得到了准确的状态。所以我建议,如果有人在问题区域中提及功能,请尝试填充您的 ios 设备,然后进行测试。以下是我针对这两个平台的最终代码。

Android代码

public double GetRemainingInternalMemoryStorage()
    {
        //Using StatFS
        var path = new StatFs(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData));
        long blockSize = path.BlockSizeLong;
        long avaliableBlocks = path.AvailableBlocksLong;
        double freeSpace = blockSize * avaliableBlocks;
        freeSpace = Math.Round(freeSpace / (1024 * 1024), 1); // Converting freespace to mb. Android follows binary pattern, so divide by 1024.
        return freeSpace;
    }

iOS代码

public double GetRemainingInternalMemoryStorage()
    {
        NSFileSystemAttributes applicationFolder = NSFileManager.DefaultManager.GetFileSystemAttributes(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
        double freeSpace = applicationFolder.FreeSize;

        freeSpace = Math.Round(freeSpace / (1000 * 1000), 1); // Converting freespace to mb. iOS follows decimal pattern, so divide by 1000

        return freeSpace;
    }