我想以编程方式检索存储信息,如容量、可用存储、应用程序总数、视频、图片等。在此先感谢..
问问题
878 次
2 回答
3
试试这些。但不能保证在非越狱设备上工作。
- (NSNumber *) totalDiskSpace
{
NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
return [fattributes objectForKey:NSFileSystemSize];
}
- (NSNumber *) freeDiskSpace
{
NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
return [fattributes objectForKey:NSFileSystemFreeSize];
}
要计算目录中的文件数(包括它的子目录,我使用了这个(这不是最有效的方法):
-(NSString *)numberOfSongs
{
NSString *musicPath = @"/var/mobile/Media/iTunes_Control/Music/";
NSArray *dirs = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:musicPath error:nil];
NSArray *subs = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:musicPath error:nil];
int totalFiles;
int subT = [subs count];
int dirT = [dirs count];
totalFiles = subT - dirT;
return [NSString stringWithFormat:@"%i", totalFiles];
}
于 2012-08-02T04:04:40.060 回答
2
看起来 WrightsCS 回答了磁盘空间问题。
如果您想要图像数量,请查看ALAssetsLibrary
(AssetsLibrary.framework
您必须在Target
设置的“将二进制文件与库链接”部分中包含此框架),然后:
#import <AssetsLibrary/AssetsLibrary.h>
// get the image assets
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSAssert(library, @"Unable to open ALAssetsLibrary");
NSUInteger __block images = 0;
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop){
NSLog(@"%@", group);
images += group.numberOfAssets;
dispatch_async(dispatch_get_main_queue(), ^{
// update my UI with the number of images
});
}
failureBlock:^(NSError *err){
NSLog(@"err=%@", err);
}];
如果您想在非越狱设备上访问 iTunes 库,请查看iPod Library Access Programming Guide,它向您展示了如何使用MPMediaQuery
(记得MediaPlayer.framework
在您的项目中包含 ),然后:
#import <MediaPlayer/MediaPlayer.h>
MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSAssert(everything, @"Unable to open MPMediaQuery");
iTunesMediaCount = [[everything items] count];
我不知道是否有用于获取应用程序数量的已发布 API。有越狱设备的解决方案,但我不知道我们其他人。
您没有问过这个问题,但是如果您想要可用的 RAM(不是闪存,而是可用于应用程序的内存),您可以通过以下方式获得它:
#import <mach/mach.h>
#import <mach/mach_host.h>
- (void)determineMemoryUsage
{
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
NSLog(@"Failed to fetch vm statistics");
/* Stats in bytes */
natural_t mem_used = (vm_stat.active_count +
vm_stat.inactive_count +
vm_stat.wire_count) * pagesize;
natural_t mem_free = vm_stat.free_count * pagesize;
natural_t mem_total = mem_used + mem_free;
// do whatever you want with mem_used, mem_free, and mem_total
}
于 2012-08-02T05:19:03.627 回答