开发者

Detect iPhone / iPod / iPad Capacity

I am creating an application w开发者_开发百科hich 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


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. :)


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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜