faroligo
faroligo

Reputation: 585

Detect iPhone / iPod / iPad Capacity

I am creating an application which needs to detect the make and model of the device it is running on (including hard disk size). I have found plenty of post's on how to identify different iPhone model's but can't find any reference to detecting the HD size.

Is this even possible?

Thanks in advance, Oli

Upvotes: 1

Views: 1025

Answers (3)

Julio Gorgé
Julio Gorgé

Reputation: 10106

The POSIX version (bit of a hack):

#include <sys/statvfs.h>

double get_disk_capacity ( char * path)
{
    struct statvfs sfs;
    unsigned long long result = 0;
    double disk_capacity = 0;

    if ( statvfs ( path, &sfs) != -1 )
    {
        result = (unsigned long long)sfs.f_frsize * sfs.f_blocks;

        if (result > 0)
        {
            disk_capacity = (double)result/(1024*1024);
        }
    }

    return disk_capacity;
}

// Sum the size of the two logical partitions (root + user space)
double total_capacity = get_disk_capacity("/") + get_disk_capacity("/private/var");

printf( "%.2f MB", total_capacity);

Upvotes: 1

faroligo
faroligo

Reputation: 585

After a bit more digging I went for the following approach as it seemed the most straight forward and seems to work fine.

Thanks to BastiBense for your solution and and I will look into POSIX further in the future.

NSDictionary *fsAttr = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
float diskSize = [[fsAttr objectForKey:NSFileSystemSize] doubleValue] / 1000000000;
NSLog(@"Disk Size: %0.0f",diskSize);

Upvotes: 1

BastiBen
BastiBen

Reputation: 19870

I think default POSIX things should be available on iOS.

You might want to take a look at statvfs (part of the standard C library) and use that to read the amount of free disk space. It's not part of the Cocoa API, but that makes sense.

Documentation should be on your machine. In Terminal do: man statvfs.

Hope that helps. Should be a 5-liner to get the information you need. :)

Upvotes: 2

Related Questions