我想通过objective C获取iPhone上每个应用程序的存储大小。
有人可以帮忙获取像这样的信息...。
有人可以帮忙获取像这样的信息...。
NSString *folderPath = "give application path here"
示例:NSString *folderPath =@"/private/var/mobile/Applications/2B63FD7F-2082-415D-A00C-F07C4BE281AA/Example.app";
以上是一个文件夹路径的示例,该路径位于iOS设备的私有目录中。在iOS开发中,很多时候需要访问这些私有目录,比如存储数据或者读取配置文件等等。因此,了解这些目录的路径非常重要。 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] fileAttributesAtPath:[folderPath stringByAppendingPathComponent:fileName] traverseLink:YES];
NSLog(@" fileDictionary %@",fileDictionary);
fileSize += [fileDictionary fileSize];
}
NSLog(@" App Name and App size: %@ : %lld", folderPath,fileSize);
如果您想查找所有应用程序的大小,请将所有应用程序路径存入数组,并编写循环语句以遍历文件夹路径。
NSArray *applicationsPath="Here array contain all applications path"
for (int i=0; i<applicationPath.count; i++)
{
NSString *folderPath = [applicationPath objectAtIndex:i]
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] fileAttributesAtPath:[folderPath stringByAppendingPathComponent:fileName] traverseLink:YES];
NSLog(@" fileDictionary %@",fileDictionary);
fileSize += [fileDictionary fileSize];
}
NSLog(@" App Name and App size: %@ : %lld", folderPath,fileSize);
}
除非设备越狱,否则您将无法以编程方式访问每个应用程序的文件夹。
这是因为每个iOS应用程序都在一个沙盒环境中运行,意味着您的应用程序只能访问此目录中的资源。如果设备已越狱,则可以删除此要求并授予您对设备磁盘的完全访问权限。