iPhone中获取磁盘空间方法
2010-08-25 17:59
387 查看
方法1,使用statfs:
参考cloudhsu代码
===============================================================
A quick Internet search to find out how to get the disk space on a device showed a number of blog posts that proposed solutions similar to this:
This approach relies on the handy Unix statfs() function that provides the required file system information. I coded it up and it worked on my 3.x devices. Fine I thought, I’m off and running. Then when testing on a 2.x device, it crashed. The problem looks to be differences in the compiler settings between the two OS versions. Rather than figure that out (I leave that as an exercise to the reader
), I continued my search and came across what I consider the “correct” solution since it uses the iPhone SDK itself, as given below.
As stated in the documentation, these interfaces wrapper the statfs() function, so they ultimately do the same thing, but they have been a part of the SDK since version 2.0! Testing verified my assumption and this approach works for me on my 2.x and 3.x devices and the numbers match what iTunes shows as well
From: http://iphoneincubator.com/blog/device-information/how-to-obtain-total-and-available-disk-space-on-your-iphone-or-ipod-touch
#import "Test1AppDelegate.h" #import <sys/param.h> #import <sys/mount.h> @implementation Test1AppDelegate @synthesize window; long freeSpace(){ struct statfs buf; long long freespace1 = -1; if(statfs("/", &buf) >= 0){ freespace1 = (long long)buf.f_bsize * buf.f_bfree; } return freespace1; } 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; } - (void)applicationDidFinishLaunching:(UIApplication *)application { freeSpace(); getTotalDiskSpaceInBytes(); // Override point for customization after application launch [window makeKeyAndVisible]; } - (void)dealloc { [window release]; [super dealloc]; } @end
参考cloudhsu代码
===============================================================
A quick Internet search to find out how to get the disk space on a device showed a number of blog posts that proposed solutions similar to this:
#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; }
This approach relies on the handy Unix statfs() function that provides the required file system information. I coded it up and it worked on my 3.x devices. Fine I thought, I’m off and running. Then when testing on a 2.x device, it crashed. The problem looks to be differences in the compiler settings between the two OS versions. Rather than figure that out (I leave that as an exercise to the reader
), I continued my search and came across what I consider the “correct” solution since it uses the iPhone SDK itself, as given below.
+(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; }
As stated in the documentation, these interfaces wrapper the statfs() function, so they ultimately do the same thing, but they have been a part of the SDK since version 2.0! Testing verified my assumption and this approach works for me on my 2.x and 3.x devices and the numbers match what iTunes shows as well
From: http://iphoneincubator.com/blog/device-information/how-to-obtain-total-and-available-disk-space-on-your-iphone-or-ipod-touch
相关文章推荐
- iPhone获取磁盘空间正规方法
- C#获取磁盘空间信息的几个方法
- linux 获取磁盘总空间的简单方法
- C#获取磁盘空间信息的几个方法
- 两种方法,获取磁盘剩余空间
- C#获取磁盘空间信息的几个方法
- linux 获取磁盘总空间的简单方法
- 两种方法,获取磁盘剩余空间--PYTHON
- Python实现获取磁盘剩余空间的2种方法
- 获取 iPhone 剩余磁盘空间的代码
- C# 获取磁盘空间大小的方法
- Delphi实现获取磁盘空间大小的方法
- 16、C++获取磁盘空间的方法
- TSQL方法获取服务器磁盘空间信息
- C#实现获取磁盘空间大小的方法
- 获取 iPhone 剩余磁盘空间的代码
- 获取iPhone 剩余磁盘空间-全部磁盘空间
- 获取 iPhone 剩余磁盘空间和全部空间的代码
- C#实现获取磁盘空间大小的方法
- iPhone中获取磁盘剩余空间大小