1

I am not able to get actual free storage space from iPhone device. I am using this link to get storage space in xamarin forms ios. Following is my code from the 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;
    }

I am working on the functionality where I need to saw user an alert if storage space is less than a threshold value. I am not getting accurate storage space so my functionality is not working.

My device has total 32 GB storage memory but when I check with above code, it saw 31989469184 bytes which is near 31.98 GB (31989469184/1000/1000/1000) which looks near to correct.But similarly Device's free space is 14.2 GB and with above code it saw 12259602432 bytes which is near 12.25 GB. I am not sure why it is giving 2 GB less.

Above linked android code works well. How can I calculate accurate free space in iOS?

enter image description here

Viral Narshana
  • 1,855
  • 2
  • 21
  • 45
  • 2
    Apple reports "size" based upon **decimal system (base 10)**, this is directly from calls to `statfs` in the OS – SushiHangover Aug 13 '20 at 18:15
  • 2
    Just to provide documentation for how apple calculates the size in case someone who stumbles on this needs it: https://support.apple.com/en-us/HT201402. Note in the article that iOS 10 and earlier used binary instead of decimal – Andrew Aug 13 '20 at 21:10
  • @SushiHangover. Ok. Thanks for your help, but even though I calculate free bytes 12259602432/1000/1000/1000 which is near 12.25 gb and not match to actual remaining space which is 14.2 GB. – Viral Narshana Aug 14 '20 at 10:13
  • @Andrew Ok. Thanks for your help, but even though I calculate free bytes 12259602432/1000/1000/1000 which is near 12.25 gb and not match to actual remaining space which is 14.2 GB. – Viral Narshana Aug 14 '20 at 10:23
  • 1
    @ViralNarshana I would assume your iOS reported 14.2g would be if all cached files were flushed, but the 12.25g reported via the actual filesystem is what is physically available at the moment. Note: iOS "can" flush any apps cache files when the space is needed by other apps – SushiHangover Aug 14 '20 at 10:45
  • @SushiHangover Is there any way to get this cached file size ? – Viral Narshana Aug 14 '20 at 18:34
  • @ViralNarshana Not that I know of personally since it requires OS-level permissions to "see" inside of each app's sandbox. – SushiHangover Aug 14 '20 at 21:40
  • @SushiHangover I am working on the functionality where I need to saw user an alert if storage space is less then a threshold value. I am not getting accurate storage space so my functionality is not working. – Viral Narshana Aug 17 '20 at 08:01
  • You can have a try with the dependency service and get the free space from the iOS project with [native methods](https://stackoverflow.com/questions/36006713/how-to-get-the-total-disk-space-and-free-disk-space-using-attributesoffilesystem). – nevermore Aug 18 '20 at 02:06

1 Answers1

1

As commented above, iOS keep some memory in cache, I filled full memory of device so that only 300 MB free space left in device. At that time I get the accurate status. So I would suggest if anyone working on functionality mention in question area try filling full your ios device and then test. Following is my final code for both platforms.

Android Code

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 Code

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;
    }
Viral Narshana
  • 1,855
  • 2
  • 21
  • 45
  • I have a quesitonon this for Android, what is the difference between doing what you have for Android and doing something like `var file = new Java.IO.File(filePath); var totalSpace = fl.UsableSpace;
return (double)totalSpace;` instead? – Kikanye May 03 '22 at 23:29