以编程方式获取iPhone上的已用空间,可用空间

时间:2012-12-24 12:03:02

标签: objective-c ios

  

可能重复:
  How to detect total available/free disk space on the iPhone/iPad device?

我想在iOS应用中获取iOS设备的以下详细信息。我在谷歌搜索过,但我对这些没有任何想法:

  1. 格式化空间
  2. 旧空间
  3. 自由空间

4 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

总计&可用空间,此方法将返回freeSpace

- (uint64_t)freeDiskspace{
    uint64_t totalSpace = 0;
    uint64_t totalFreeSpace = 0;

    __autoreleasing NSError *error = nil;  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];  

   if (dictionary) {  
       NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];  
       NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
      totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
      totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
      NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
   }
   else {  
      NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %d", [error domain], [error code]);  
   }  

   return totalFreeSpace;
}

答案 2 :(得分:0)

您可以使用uidevice-extension获取这些内容 您需要的是以下功能:

- (NSNumber *) totalDiskSpace;
- (NSNumber *) freeDiskSpace;

来自UIDevice-Hardware.h

答案 3 :(得分:-1)

您也可以使用NSFileManager和方法

- (NSDictionary *)attributesOfFileSystemForPath:(NSString *)path error:(NSError **)error

在您要发送数据的路径上。此方法使用statfs(在其他答案中提到)。

相关问题