我看过很多关于如何获取iOS设备有多少可用空间,或者iOS设备有多少可用空间的帖子,但是有没有办法确定应用程序本身使用了多少空间? (包括应用程序本身及其所有资源/文档/缓存/等)。这与在“设置”->“常规”->“iPhone 存储”中看到的值相同。
我最终弄清楚了如何做到这一点:
我创建了一个类别NSFileManager
并补充道:
-(NSUInteger)applicationSize
NSString *appgroup = @"Your App Group"; // Might not be necessary in your case.
NSURL *appGroupURL = [self containerURLForSecurityApplicationGroupIdentifier:appgroup];
NSURL *documentsURL = [[self URLsForDirectory: NSDocumentDirectory inDomains: NSUserDomainMask] firstObject];
NSURL *cachesURL = [[self URLsForDirectory: NSCachesDirectory inDomains: NSUserDomainMask] firstObject];
NSUInteger appGroupSize = [appGroupURL fileSize];
NSUInteger documentsSize = [documentsURL fileSize];
NSUInteger cachesSize = [cachesURL fileSize];
NSUInteger bundleSize = [[[NSBundle mainBundle] bundleURL] fileSize];
return appGroupSize + documentsSize + cachesSize + bundleSize;
}
我还添加了一个类别NSURL
具有以下内容:
-(NSUInteger)fileSize
{
BOOL isDir = NO;
[[NSFileManager defaultManager] fileExistsAtPath:self.path isDirectory:&isDir];
if (isDir)
return [self directorySize];
else
return [[[[NSFileManager defaultManager] attributesOfItemAtPath:self.path error:nil] objectForKey:NSFileSize] unsignedIntegerValue];
}
-(NSUInteger)directorySize
{
NSUInteger result = 0;
NSArray *properties = @[NSURLLocalizedNameKey, NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:self includingPropertiesForKeys:properties options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
for (NSURL *url in files)
{
result += [url fileSize];
}
return result;
}
如果您有大量应用程序数据,则运行需要一些时间,但它可以工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)