美文网首页api-UIiosUi
iOS 获取磁盘空间总大小 剩余大小 已使用大小 等

iOS 获取磁盘空间总大小 剩余大小 已使用大小 等

作者: 清辉_ | 来源:发表于2021-07-21 10:33 被阅读0次

-(float)getFreeDiskspace {
    float totalSpace;
    float totalFreeSpace;
    float totalUsedSpace;
    
    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 floatValue];
        totalFreeSpace = [freeFileSystemSizeInBytes floatValue];
        totalUsedSpace = totalSpace - totalFreeSpace;
        
        float freePercent = totalFreeSpace/totalSpace;
        float usedPercent = totalUsedSpace/totalSpace;
        
        NSLog(@"iphone磁盘未使用率 =%@", [[NSString stringWithFormat:@"%.2f",freePercent*100]    stringByAppendingString:@"%"]);
        NSLog(@"iphone磁盘使用率  =%@",  [[NSString stringWithFormat:@"%.2f",usedPercent*100]    stringByAppendingString:@"%"]);
        NSLog(@"iphone磁盘总大小  =%@",  [[NSString stringWithFormat:@"%.2f",((totalSpace/1000.0f)/1000.0f/1000.0f)]     stringByAppendingString:@"GB"]);
        NSLog(@"iphone磁盘已使用大小    =%@",  [[NSString stringWithFormat:@"%.2f",((totalUsedSpace/1000.0f)/1000.0f/1000.0f)] stringByAppendingString:@"GB"]);
        NSLog(@"iphone磁盘剩余大小    =%@",  [[NSString stringWithFormat:@"%.2f",((totalFreeSpace/1000.0f)/1000.0f/1000.0f)] stringByAppendingString:@"GB"]);
        
        NSLog(@"Memory Capacity of %f GB with %f GB Free memory available.", ((totalSpace/1000.0f)/1000.0f/1000.0f), ((totalFreeSpace/1000.0f)/1000.0f)/1000.0f);
    } else {
        NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);
    }
    return totalFreeSpace;
}




//注意 // 这里初始到的值的单位是bytes,iOS、android的存储空间是这样算的1000MB = 1,1000进制算的 

相关文章

网友评论

    本文标题:iOS 获取磁盘空间总大小 剩余大小 已使用大小 等

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