api-UIiosUi

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

2021-07-21  本文已影响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进制算的 

上一篇 下一篇

猜你喜欢

热点阅读