我读了很多关于它的信息,做了一些测试并得到错误的结果,我觉得我错过了一些东西。 我得到的唯一信息,它是正确的信息: DEVICE可用空间/总空间。关于 - ios当前应用程序存储的2个问题
但我只需要关于我的APP的信息。所以:
- 如何获取iOS应用程序的存储大小。显示在设置上的 。 (不是其他应用程序,当前应用程序)
- 如何删除所有缓存/ tmp /其他文件,因此存储大小将与第一次安装时的大小相同?
谢谢。
我读了很多关于它的信息,做了一些测试并得到错误的结果,我觉得我错过了一些东西。 我得到的唯一信息,它是正确的信息: DEVICE可用空间/总空间。关于 - ios当前应用程序存储的2个问题
但我只需要关于我的APP的信息。所以:
谢谢。
您可以使用此找到您的应用程序缓存文件,然后删除它们:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *firstPath = paths[0];
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:firstPath error:nil];
NSEnumerator *contentsEnumurator = [contents objectEnumerator];
NSString *file;
unsigned long long int folderSize = 0;
while (file = [contentsEnumurator nextObject]) {
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[firstPath stringByAppendingPathComponent:file] error:nil];
folderSize += [[fileAttributes objectForKey:NSFileSize] intValue];
}
folderSize = folderSize/1000.0f/1000.0f; // Folder size in MB (more pressition folderSize/1024.0f/1024.0f);
NSMutableArray *filesToRemove = [[NSMutableArray alloc] init];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:firstPath error:NULL];
for (NSString *path in files) {
BOOL isDir;
NSString *filePath = [[[self class] cachesDirectoryPath] stringByAppendingPathComponent:path];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir];
if (fileExists && !isDir) {
NSString *filePath = [NSString stringWithFormat:@"%@/%@", firstPath ,path];
NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath: filePath error:NULL];
NSDictionary *fileDescription = [[NSDictionary alloc] init];
@try {
//Adds the file description to the files to remove
fileDescription = @{
@"kFilePath": filePath,
@"kFileSize": [attr objectForKey:NSFileSize],
@"kFileModificationDate": [attr objectForKey:NSFileModificationDate]
};
} @catch (NSException *exception) {
NSLog(@"File description problem");
}
if (fileDescription.allKeys.count > 0) {
[filesToRemove addObject:fileDescription];
}
}
}
float removedMB = 0;
for (NSDictionary *fileDescription in filesToRemove) {
NSLog(@"Removing file: %@", [fileDescription objectForKey:@"kFilePath"]);
removedMB += [((NSNumber *)[fileDescription objectForKey:@"kFileSize"]) floatValue]/1000.0f/1000.0f;
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:[fileDescription objectForKey:@"kFilePath"] error:&error];
}
NSLog(@"Removed MB: %f", removedMB);
(请阅读全文)嘿老兄用于检查特定应用程序的大小,只要按照下列步骤 1,打开设置 2,然后一般 3抽头存储& icloud的使用 这里你看到的两件事情1的存储和2- icloud的都有3个选项 1 used- 2服务现有 3管理存储 - 你只需轻按上管理存储从存储在这里你会se e所有已用和可用的存储空间 ,如果您检查其上的特定应用程序磁带的大小,则会看到此应用程序的大小和数据大小,通常您会在安装之前从应用商店检查应用程序的大小。 这里也有选择删除应用程序,如果你点击它,你将删除应用程序以及它的数据也删除
对于第一个问题,下面的代码将工作。但我不知道这种方法是否符合Apple的API使用。
// Get block size on user partition.
struct statfs *mntbufp = NULL;
getmntinfo(&mntbufp, 0);
int i, count = 0;
unsigned int blockSize;
count = getmntinfo(&mntbufp, 0);
for (i = 0; i < count; ++i)
{
// Find users partition.
if (strcmp(mntbufp[i].f_mntonname, "/private/var") == 0) {
blockSize = mntbufp[i].f_bsize;
break;
}
}
//get full pathname of bundle directory
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
//get paths of all of the contained subdirectories
NSArray *bundleArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:bundlePath error:nil];
//to access each object in array
NSEnumerator *filesEnumerator = [bundleArray objectEnumerator];
NSString *fileName;
unsigned long long int fileSize = 0;
unsigned long long int filesSize = 0;
unsigned long long int sizeOnDisk = 0;
NSError *error = nil;
//return next object from enumerator
while (fileName = [filesEnumerator nextObject]) {
NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:[bundlePath stringByAppendingPathComponent:fileName] error:&error];
fileSize = [fileDictionary fileSize];
// File size is sum of all file sizes.
filesSize += fileSize;
// Calculate size on disk.
sizeOnDisk += ceil((double)fileSize/blockSize) * blockSize;
}
// Find sandbox path.
NSString *sandboxPath = NSHomeDirectory();
NSArray *sandboxArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:sandboxPath error:nil];
filesEnumerator = [sandboxArray objectEnumerator];
while (fileName = [filesEnumerator nextObject]) {
NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:[sandboxPath stringByAppendingPathComponent:fileName] error:&error];
fileSize = [fileDictionary fileSize];
filesSize += fileSize;
sizeOnDisk += ceil((double)fileSize/blockSize) * blockSize;
}
//converts a byte count value into a textual representation that is formatted with the appropriate byte modifier (KB, MB, GB and so on)
NSString *fileSizeStr = [NSByteCountFormatter stringFromByteCount:filesSize countStyle:NSByteCountFormatterCountStyleBinary];
NSString *sizeOnDiskStr = [NSByteCountFormatter stringFromByteCount:sizeOnDisk countStyle:NSByteCountFormatterCountStyleBinary];`
这会计算整个包的大小。部分功劳归功于this stackoverflow question中的1218GG。但他的方法不以二进制格式计数(iOS使用二进制系统; Mac OS 10.6和更高版本使用基本10系统,请参阅this article),并且不计算沙盒文件的大小。我修改/添加了相应的代码。
这里是文件大小iOS应用计算和所述一个Mac的取景器显示之间的比较(取景器显示与应用程序计算):
61801乙与61 KB; 668 KB(637,472 B)与623 KB; 13.5 MB(13,520,085 B)与13.4 MB
有一些变化。 可能由于Mac OS计算文件大小的方式和iOS计算文件大小之间的差异。在iPhone上运行iOS 9时,系统显示204 KB,而我的方法计数为208 KB。当应用尺寸大于10 MB时,可以忽略该差异。
看来,iOS系统显示的是“磁盘大小”而不是“文件大小”。所以文件大小和sizeOnDisk之间的转换是必要的。目前的应用程序存储是'sizeOnDisk'。 –
iOS应用程序拥有自己的沙盒并写入他们自己的文档目录。只需删除应用程序文档目录的内容即可清除所有内容。 “应用程序存储大小”是什么意思? – NSNoob
当您转到设置应用程序,存储和iCloud使用情况时,您会看到应用程序列表和存储大小。 我想获得代码,我的应用程序存储大小的值。 关于您在文档目录上所说的内容,我不确定是否足够。 的含义 - 如果我删除文档文件夹,应用程序存储大小将不会与首次安装(甚至关闭)相同。还有缓存文件夹等。 这就是为什么我在这里问,也许有人知道要删除的文件夹的完整列表。 – user1105951
用于清除您可以使用folderPath的所有数据。按照http://stackoverflow.com/a/9358551/5215474 – Saranjith