objective-cmacoscocoastorage

How to get storage capacity of mac programmatically?


How do I get the total capacity, available space and free space in Cocoa?

As shown in the screenshot, I want to get this programmatically in my Cocoa application for macOS.

enter image description here


Solution

  • I got my answer from the link.

    So I am posting what I did using that reference.

        NSError *error;
    
        NSFileManager *fm = [NSFileManager defaultManager];
        NSDictionary *attr = [fm attributesOfFileSystemForPath:@"/"
                                                         error:&error];
        NSLog(@"Attr: %@", attr);
        float totalsizeGb = [[attr objectForKey:NSFileSystemSize]floatValue] / 1000000000;
        NSLog(@" size in GB %.2f",totalsizeGb);
    
        float freesizeGb = [[attr objectForKey:NSFileSystemFreeSize]floatValue] / 1000000000;
        NSLog(@" size in GB %.2f",freesizeGb);
    

    Hope that helps anyone else also.

    Thanks...