NSString* free = [self freeDiskSpaceInGB];
NSLog(@"free:%@ GB",free);
- (long)freeDiskSpaceInBytes {
if (@available(iOS 11.0, *)) {
[NSURL alloc];
NSURL * url = [[NSURL alloc]initFileURLWithPath:[NSString stringWithFormat:@"%@",NSHomeDirectory()]];
NSError * error = nil;
NSDictionary<NSURLResourceKey, id> * dict = [url resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:&error];
if (error) {
return 0;
}
long long space = [dict[NSURLVolumeAvailableCapacityForImportantUsageKey] longLongValue];
return space;
} else {
NSError * error = nil;
NSDictionary<NSFileAttributeKey, id> * systemAttributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (error) {
return 0;
}
long long space = [systemAttributes[NSFileSystemFreeSize] longLongValue];
return space;
}
}
- (NSString *)freeDiskSpaceInGB
{
return [NSByteCountFormatter stringFromByteCount:[self freeDiskSpaceInBytes] countStyle:NSByteCountFormatterCountStyleDecimal];
}```
网友评论