有没有办法检测应用程序使用的磁盘 space?
Is there a way to detect used disk space for the application?
我需要检查我的应用程序是否使用超过 300mb 的磁盘 space。我该怎么做?
使用this answer查找文件夹的大小
- (unsigned long long int)folderSize:(NSString *)folderPath {
NSArray *filesArray =
[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:folderPath
error:nil];
NSEnumerator *filesEnumerator = [filesArray objectEnumerator];
NSString *fileName;
unsigned long long int fileSize = 0;
while (fileName = [filesEnumerator nextObject]) {
NSDictionary *fileDictionary =
[[NSFileManager defaultManager]
attributesOfItemAtPath:[folderPath stringByAppendingPathComponent:fileName]
error:nil];
fileSize += [fileDictionary fileSize];
}
return fileSize;
}
称之为
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *documentsDirectory =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
NSString *cachesDirectory =
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
unsigned long long int bundleSize = [self folderSize:bundlePath];
unsigned long long int docsSize = [self folderSize:documentsDirectory];
unsigned long long int cachesSize = [self folderSize:cachesDirectory];
long double totalSize = (bundleSize + docsSize + cachesSize)/1024.0/1024.0;
这将为您提供应用程序包、文档目录和缓存目录的大小(以兆字节为单位)。
我需要检查我的应用程序是否使用超过 300mb 的磁盘 space。我该怎么做?
使用this answer查找文件夹的大小
- (unsigned long long int)folderSize:(NSString *)folderPath {
NSArray *filesArray =
[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:folderPath
error:nil];
NSEnumerator *filesEnumerator = [filesArray objectEnumerator];
NSString *fileName;
unsigned long long int fileSize = 0;
while (fileName = [filesEnumerator nextObject]) {
NSDictionary *fileDictionary =
[[NSFileManager defaultManager]
attributesOfItemAtPath:[folderPath stringByAppendingPathComponent:fileName]
error:nil];
fileSize += [fileDictionary fileSize];
}
return fileSize;
}
称之为
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *documentsDirectory =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
NSString *cachesDirectory =
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
unsigned long long int bundleSize = [self folderSize:bundlePath];
unsigned long long int docsSize = [self folderSize:documentsDirectory];
unsigned long long int cachesSize = [self folderSize:cachesDirectory];
long double totalSize = (bundleSize + docsSize + cachesSize)/1024.0/1024.0;
这将为您提供应用程序包、文档目录和缓存目录的大小(以兆字节为单位)。