Get disk size
//Get Hard disk size+(cgfloat) getdisksize{nsstring*libpath = [Nssearchpathfordirectoriesindomains (nslibrarydirectory, Nsuserdomainmask, YES) ObjectAtIndex:0]; NSLog (@"%@", LibPath); //Initialize File ManagerNsfilemanager *manager =[Nsfilemanager Defaultmanager]; //get all paths under LibNsarray *liballpath =[manager Subpathsatpath:libpath]; Nsinteger size=0; for(NSString *pathinchLiballpath) { if(! [Path containsstring:@"Preferences"]) { //stitch The path together in fullNSString *patha =[LibPath Stringbyappendingpathcomponent:path]; //get all information about a fileNsdictionary *fileattri =[manager Attributesofitematpath:patha Error:nil]; //Get File SizeSize + = [fileattri[@"nsfilesize"] IntegerValue]; } } returnsize/1024.0/1024.0;}
Ways to clear the cache
//Clear Cache+ (void) cleardisk{NSString*libpath = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES) [0]; //Initialize File ManagerNsfilemanager *mgr =[Nsfilemanager Defaultmanager]; //get all paths under LibNsarray *liballpath =[Mgr Subpathsatpath:libpath]; for(NSString *pathinchLiballpath) { if(! [Path containsstring:@"Preferences"]) { //stitch The path together in fullNSString *patha =[LibPath Stringbyappendingpathcomponent:path]; //Remove files from[Mgr Removeitematpath:patha Error:nil]; } }}
The garbage size is displayed in the cell that clears the cache when called
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *cellidentifier =@"Cell"; UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:@"Cell"]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:cellidentifier]; } Switch(indexpath.row) { Case 0: Cell.textLabel.text=@"Event Collection"; Break; Case 1: Cell.textLabel.text=@"Movie Collection"; Break; Case 2: {cell.textLabel.text=@"Clear Cache"; NSString*size = [NSString stringWithFormat:@"%.2fm", [Cleardiskmanager Getdisksize]]; Cell.detailTextLabel.text=size; Break; } default: Break; } Cell.backgroundcolor=[Uicolor Cyancolor]; returncell;}
Clean up cache when clicking cell
- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{Switch(indexpath.row) { Case 1: {Listtableviewcontroller*LISTVC =[[Listtableviewcontroller alloc] init]; [Self.navigationcontroller PUSHVIEWCONTROLLER:LISTVC Animated:yes]; Break; } Case 0: {Activitytableviewcontroller*ACTIVITYVC =[[Activitytableviewcontroller alloc] init]; [Self.navigationcontroller PUSHVIEWCONTROLLER:ACTIVITYVC Animated:yes]; Break; } Case 2: {Uialertcontroller*alercontroller = [Uialertcontroller alertcontrollerwithtitle:@"Tips"Message@"It may take a bit of your traffic to clear the cache"Preferredstyle:uialertcontrollerstylealert]; Uialertaction*action = [Uialertaction actionwithtitle:@"Cancel"Style:uialertactionstyledefault handler:^ (Uialertaction *_nonnull Action) {[self dismissviewcontrolleranimated:yes completion:nil]; }]; Uialertaction*action1 = [Uialertaction actionwithtitle:@"Determine"Style:uialertactionstyledefault handler:^ (Uialertaction *_nonnull Action) {[Cleardiskmanager cleardisk]; [Self.tableview Reloaddata]; }]; [Alercontroller addaction:action]; [Alercontroller Addaction:action1]; [Self Presentviewcontroller:alercontroller animated:yes completion:nil]; } default: Break; }}
Class methods for encapsulating caches