Viral Narshana
Viral Narshana

Reputation: 1877

Get ios internal storage memory information xamarin forms ios

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

Upvotes: 1

Views: 802

Answers (1)

Viral Narshana
Viral Narshana

Reputation: 1877

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;
    }

Upvotes: 1

Related Questions