diskspacetool.h// screenrecordmaster//// Created by Feimo on 15/7/24.// Copyright (c) 2015 Feimo. All rights reserved.//#import <Foundation/Foundation.h> #include <sys/param.h> #include <sys/mount.h > @interface diskspacetool:nsobject+ (nsstring *) freediskspaceinbytes;//Phone remaining space + (NSString *) totaldiskspaceinbytes ///Mobile total space + (NSString *) Foldersizeatpath: (nsstring*) folderpath;//a folder takes up the size of the space @end
diskspacetool.m//screenrecordmaster////Created by Feimo on 15/7/24.//Copyright (c) 2015 Feimo. All rights reserved.//#import "DiskSpaceTool.h" @implementation diskspacetool//phone remaining space + (NSString *) freediskspaceinbytes{struct STATFS buf; Long Long freespace =-1; if (Statfs ("/var", &buf) >= 0) {FreeSpace = (long Long) (buf.f_bsize * buf.f_bfree); } return [self humanreadablestringfrombytes:freespace]; }//mobile Phone Total space + (NSString *) totaldiskspaceinbytes{struct statfs buf; Long long freespace = 0; if (Statfs ("/", &buf) >= 0) {FreeSpace = (long Long) buf.f_bsize * buf.f_blocks; } if (Statfs ("/private/var", &buf) >= 0) {freespace + = (long long) buf.f_bsize * buf.f_blocks; } printf ("%lld\n", freespace); return [self humanreadablestringfrombytes:freespace];} Traverse folder to get folder size + (NSString *) Foldersizeatpath: (nsstring*) folderpath{nsfilemanager* manager = [Nsfilemanager Defaultma Nager]; if (![ Manager FileexIstsatpath:folderpath]) return 0; Nsenumerator *childfilesenumerator = [[manager Subpathsatpath:folderpath] objectenumerator]; nsstring* FileName; Long long foldersize = 0; while ((FileName = [Childfilesenumerator nextobject]) = nil) {nsstring* Fileabsolutepath = [FolderPath Stringbyapp Endingpathcomponent:filename]; Foldersize + = [self filesizeatpath:fileabsolutepath]; } return [self humanreadablestringfrombytes:foldersize];} Size of individual file + (long Long) Filesizeatpath: (nsstring*) filepath{nsfilemanager* manager = [Nsfilemanager Defaultmanager]; if ([manager Fileexistsatpath:filepath]) {return [[manager Attributesofitematpath:filepath Error:nil] fileSize]; } return 0;} Calculate file Size + (NSString *) Humanreadablestringfrombytes: (unsigned long long) bytecount{float numberofbytes = byteCount; int multiplyfactor = 0; Nsarray *tokens = [nsarray arraywithobjects:@ "bytes", @ "KB", @ "MB", @ "GB", @ "TB", @ "PB", @ "EB", @ "ZB", @ "YB", nil]; while (numbErofbytes > 1024x768) {numberofbytes/= 1024; multiplyfactor++; } return [NSString stringwithformat:@ "%4.2f%@", numberofbytes, [tokens Objectatindex:multiplyfactor]];} @end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS gets the size of the storage space in the folder, the size of the phone's remaining space, the total phone space size tool