【发布时间】:2019-05-07 08:17:51
【问题描述】:
我尝试在目标 C 中检查 iOS 9 及更高版本的可用空间。 对于 iOS 11,这很简单:NSURLVolumeAvailableCapacityForImportantUsageKey 它工作正常!我得到 26GB。
但对于 iOS 9 / 10,我不知道该怎么做。 我试试这个功能:
//Get free space on the mobile
-(uint64_t)getFreeDiskspace {
uint64_t totalSpace = 0;
uint64_t totalFreeSpace = 0;
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 unsignedLongLongValue];
totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
} else {
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
}
return totalFreeSpace;
}
但结果是错误的,同一设备我得到了 18GB。
你有什么想法吗?
谢谢你:)
【问题讨论】:
标签: objective-c ios9 ios10