美文网首页
iOS获取存储空间大小

iOS获取存储空间大小

作者: 游戏创作者 | 来源:发表于2022-05-05 15:22 被阅读0次
    • 获取设备总的存储空间:
    - (float)getTotalDiskSpace{
        float totalsize = 0.0;
        NSError *error = nil;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
        if (dictionary)
        {
           NSNumber *_total = [dictionary objectForKey:NSFileSystemSize];
           totalsize = [_total unsignedLongLongValue]*1.0;
        } else
        {
           NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
        }
        return totalsize;
    }
    
    • 获取设备可用存储空间:
    -(float)getFreeDiskSpace{
        float freesize = 0.0;
        NSError *error = nil;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
        if (dictionary)
        {
           NSNumber *_free = [dictionary objectForKey:NSFileSystemFreeSize];
           freesize = [_free unsignedLongLongValue]*1.0;
        } else
        {
           NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
        }
        return  freesize;
    }
    

    调用:

    float total = [self getTotalDiskSpace];
    NSLog(@"total:%f GB",total/(1024*1024*1024));
        
    float free = [self getFreeDiskSpace];
    NSLog(@"free:%f GB",free/(1024*1024*1024));
    

    相关文章

      网友评论

          本文标题:iOS获取存储空间大小

          本文链接:https://www.haomeiwen.com/subject/jrtoyrtx.html