iOS获取系统磁盘大小-NSFileManager

2020-11-23  本文已影响0人  东方诗空
参见NSFileManager的头文件尾部定义的字段,不难发现还有许多可以获取的信息的。

具体的都可以根据需求来获取。

FOUNDATION_EXPORT NSString * const NSFileType;
FOUNDATION_EXPORT NSString     * const NSFileTypeDirectory;
FOUNDATION_EXPORT NSString     * const NSFileTypeRegular;
FOUNDATION_EXPORT NSString     * const NSFileTypeSymbolicLink;
FOUNDATION_EXPORT NSString     * const NSFileTypeSocket;
FOUNDATION_EXPORT NSString     * const NSFileTypeCharacterSpecial;
FOUNDATION_EXPORT NSString     * const NSFileTypeBlockSpecial;
FOUNDATION_EXPORT NSString     * const NSFileTypeUnknown;
FOUNDATION_EXPORT NSString * const NSFileSize;
FOUNDATION_EXPORT NSString * const NSFileModificationDate;
FOUNDATION_EXPORT NSString * const NSFileReferenceCount;
FOUNDATION_EXPORT NSString * const NSFileDeviceIdentifier;
FOUNDATION_EXPORT NSString * const NSFileOwnerAccountName;
FOUNDATION_EXPORT NSString * const NSFileGroupOwnerAccountName;
FOUNDATION_EXPORT NSString * const NSFilePosixPermissions;
FOUNDATION_EXPORT NSString * const NSFileSystemNumber;
FOUNDATION_EXPORT NSString * const NSFileSystemFileNumber;
FOUNDATION_EXPORT NSString * const NSFileExtensionHidden;
FOUNDATION_EXPORT NSString * const NSFileHFSCreatorCode;
FOUNDATION_EXPORT NSString * const NSFileHFSTypeCode;
#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_2_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
FOUNDATION_EXPORT NSString * const NSFileImmutable;
FOUNDATION_EXPORT NSString * const NSFileAppendOnly;
FOUNDATION_EXPORT NSString * const NSFileCreationDate;
FOUNDATION_EXPORT NSString * const NSFileOwnerAccountID;
FOUNDATION_EXPORT NSString * const NSFileGroupOwnerAccountID;
#endif
#if MAC_OS_X_VERSION_10_4 <= MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_2_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
FOUNDATION_EXPORT NSString * const NSFileBusy;
#endif
#if __IPHONE_4_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
FOUNDATION_EXPORT NSString * const NSFileProtectionKey NS_AVAILABLE(NA, 4_0);
FOUNDATION_EXPORT NSString     * const NSFileProtectionNone NS_AVAILABLE(NA, 4_0);
FOUNDATION_EXPORT NSString     * const NSFileProtectionComplete NS_AVAILABLE(NA, 4_0);
#endif

FOUNDATION_EXPORT NSString * const NSFileSystemSize;
FOUNDATION_EXPORT NSString * const NSFileSystemFreeSize;
FOUNDATION_EXPORT NSString * const NSFileSystemNodes;
FOUNDATION_EXPORT NSString * const NSFileSystemFreeNodes;
+(float)getFreeDiskspace {
    float totalSpace;
    float totalFreeSpace;
    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];
        NSLog(@"总容量 %f GB 可用容量 %f GB ", ((totalSpace/1024.0f)/1024.0f/1024.0f), ((totalFreeSpace/1024.0f)/1024.0f)/1024.0f);
    } else {

    }
    return totalFreeSpace;
}
上一篇下一篇

猜你喜欢

热点阅读