2011年12月21日 星期三

偵測iOS還剩下多少記憶體的程式碼(iOS - detect total available/free disk space on the iPhone/iPad device)


-(float)getFreeDiskspace {
float totalSpace = 0.0f;
float totalFreeSpace = 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];  NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
totalSpace = [fileSystemSizeInBytes floatValue];
totalFreeSpace = [freeFileSystemSizeInBytes floatValue];
NSLog(@"Memory Capacity of %f MiB with %f MiB Free memory available.", ((totalSpace/1024.0f)/1024.0f), ((totalFreeSpace/1024.0f)/1024.0f));
} else {  NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);  }  
return totalFreeSpace;
}