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

Source: Internet
Author: User

  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.