-1
我需要檢查我的應用程序是否使用超過300mb的磁盤空間。我怎樣才能做到這一點?有沒有辦法檢測應用程序使用的磁盤空間?
我需要檢查我的應用程序是否使用超過300mb的磁盤空間。我怎樣才能做到這一點?有沒有辦法檢測應用程序使用的磁盤空間?
使用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;
這會給您的應用程序的包,文件目錄的大小,以兆字節緩存目錄。