Mag 07
Navigando sulla rete si trovano una marea di post con il seguente codice per ottenere la soluzione:
#include <sys/param.h> #include <sys/mount.h> +(float)getTotalDiskSpaceInBytes { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); struct statfs tStats; statfs([[paths lastObject] cString], &tStats); float totalSpace = (float)(tStats.f_blocks * tStats.f_bsize); return totalSpace; }
Questo approccio sfrutta la funzione statfs() di Unix per ottenere le informazioni del filesystem. Andrebbe bene se utilizzassimo solamente i dispositivi con versione del sistema operativo 3.x. Infatti per la versione 2.x il sistema “crashed”.
La soluzione corretta la suggerisce la documentazione dove troviamo il seguente pezzo di codice:
+(float)getTotalDiskSpaceInBytes { float totalSpace = 0.0f; NSError *error = nil; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error]; if (dictionary) { NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize]; totalSpace = [fileSystemSizeInBytes floatValue]; } else { DLog(@"Error Obtaining File System Info: Domain = %@, Code = %@", [error domain], [error code]); } return totalSpace; }
Il gioco è fatto!
Ultimi Commenti