Search code examples
iosxamarinxamarin.formsxamarin.iosxamarin-studio

How to get free internal storage space using Xamarin IOS


I can get RAM details using (NSProcessInfo.ProcessInfo.PhysicalMemory). But i want to get free internal device storage using Xamarin IOS.


Solution

  • The method for getting the internal free space is this one:

      NSFileManager.DefaultManager.GetFileSystemAttributes (Environment.GetFolderPath (Environment.SpecialFolder.Personal)).FreeSize;
    

    If you are using Xamarin.Forms, you can make a custom interface

    namespace Your.Namespace.Interfaces
    {
        public interface IStorageInterface
        {
            double GetFreeSpace(); //Not sure about the return type, try long, or double
    
        }
    }
    

    In your iOS Project:

    [assembly: Xamarin.Forms.Dependency(typeof(Your.Namespace.iOS.StorageRenderer))]
    namespace Your.Namespace.iOS
    {
        public class StorageRenderer : IStorageInterface
        {
            public double GetFreeSpace()
            {
                return NSFileManager.DefaultManager.GetFileSystemAttributes (Environment.GetFolderPath (Environment.SpecialFolder.Personal)).FreeSize;
            }
    
        }
    }