获取iOS设备总空间及可用空间大小

2020-09-27  本文已影响0人  oldmonster

1、获取总空间(单位:G)

+ (CGFloat)totalDiskSpace {
    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];
        return  [total unsignedLongLongValue] * 1.0/1024.0/1024.0/1024.0;
    }
    return 0;
}

2、获取可用空间大小(单位:M)

+ (CGFloat)freeDiskSpace {
    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];
        return [free unsignedLongLongValue] * 1.0/1024.0/1024.0;
    }
    return 0;
}

上一篇 下一篇

猜你喜欢

热点阅读