Code to delete files under iOS system

Source: Internet
Author: User
Tags create directory md5 md5 encryption reserved


This article summed up a few in the iOS system to delete the file code, very practical, the need for small partners can refer to.



Method One: This section of Objective C code is used to delete the file of the specified path




if ([FileManager removeitematpath:@ "FilePath" Error:null]) {NSLog (@ "removed successfully");}


Method Two:




Nsfilemanager *defaultmanager;   Defaultmanager = [Nsfilemanager Defaultmanager]; [Defaultmanager Removefileatpath:tildefilename Handler:nil];


Handler can receive messages, such as if the deletion fails, you can use Filemanager:shouldproceedaftererror:.



Method Three:



IOS Delete File Delete folder Create file Create folder determine file exists MD5 encapsulation class



I have recently been using data access and deletion, so I wrote a functional class, their own use or quite convenient to share again



storagedata.m




STORAGEDATA.M//XUNYI7////Created by David on 13-6-28. Copyright (c) 2013 David. All rights reserved.   #import <CommonCrypto/CommonDigest.h> #import "StorageData.h" #import "xunYi7AppDelegate.h"   @ Implementation Storagedata  -(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{ NSLog (@ "Start knot didreceivedata search Data"); }  -(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{NSLog (@ " Start knot didreceiveresponse search Data "); }  -(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{NSLog (@ "Didfailwitherror "); }  -(void) connectiondidfinishloading: (nsurlconnection *) connection{NSLog (@ "connectiondidfinishloading");   + (Nsmutabledata *) Remotefetchdata: (NSString *) dataurl{nsstring *currentdatafilepath = [[Self datapath] Stringbyappendingpathcomponent:[self Fetchtodaydate]];  //Create directory Currentdatafilepath = [self createdirectory:currentdatafilepath];   Currentdatafilepath = [Currentdatafilepath stringbyappendingpathcomponent:[nsstring stringWithFormat:@%@. Plist ", [self md5:dataurl]]];   if ([Xunyi7appdelegate isreachable]) {Nsurl *url = [[Nsurl alloc] initwithstring:dataurl]; Nsurlrequest *request = [[Nsurlrequest alloc] Initwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy TIMEOUTINTERVAL:60];   Nsurlresponse *response = [[Nsurlresponse alloc] init]; Nserror *receivedataerror = [[Nserror alloc] init];   Nsmutabledata *receiveddata = (Nsmutabledata *) [Nsurlconnection sendsynchronousrequest:request Returningresponse:&response error:&receivedataerror]; [UIApplication sharedapplication].networkactivityindicatorvisible = NO;   return receiveddata; }else{[Xunyi7appdelegate shownetworkmessage];}   return nil; }   + (Nsmutabledata *) Localfetchdata: (NSString *) dataurl{  NSString *currentdatafilepath = [[Self datapath] St Ringbyappendingpathcomponent:[self Fetchtodaydate]]; NSString*yesterdaydatafilepath = [[self datapath] stringbyappendingpathcomponent:[self fetchyesterdaydate]];  //Create directory Currentdatafilepath = [self createdirectory:currentdatafilepath];   Currentdatafilepath = [Currentdatafilepath stringbyappendingpathcomponent:[nsstring stringWithFormat:@%@. Plist ", [self md5:dataurl]]]; Yesterdaydatafilepath = [Yesterdaydatafilepath stringbyappendingpathcomponent:[nsstring stringWithFormat:@%@. Plist ", [self md5:dataurl]]];   Nsmutabledata *localdata = [self fromfilenamepathfetchlocaldata:currentdatafilepath];   if (localdata!= nil) {//Local data return localdata  }else{//remote fetch data   nsmutabledata *receiveddata = [self remo Tefetchdata:dataurl];   if (receiveddata!= nil) {if ([self storagedatatofile:receiveddata filename:currentdatafilepath]) {NSLog (@ Save successful) ); [Self removedirectory]; }else{NSLog (@ save failed);} else{if ((Localdata = [self Fromfilenamepathfetchlocaldata:yesterdaydatafilepath])!= nil) {return localdata;}} return R EceiveddaTa return nil; }  //MD5 Encryption string + (NSString *) MD5: (NSString *) str{const char *CSTR = [str utf8string]; unsigned char result[16]; CC_MD5 (CStr, strlen (CStr), result); This is the MD5 called return [NSString stringWithFormat: @ "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%0 2x%02x ", Result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], re SULT[10], result[11], result[12], result[13], result[14], result[15]; //Upload picture storage + (void) Saveuploadimage: (UIImage *) image withname: (NSString *) imagename{nsfilemanager = [[ Nsfilemanager alloc] init]; Nserror *error;  //Get sandbox directory nsstring *fullpath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]; FullPath = [FullPath stringbyappendingpathcomponent:@ "Tmpimage"]; if (![ FileManager Fileexistsatpath:fullpath]) {[FileManager Createdirectoryatpath:fullpath withIntermediateDirectories: YES Attributes:nil error:&error]; }   FullPath = [FullPath strIngbyappendingpathcomponent:imagename]; NSData *imagedata = uiimagejpegrepresentation (image, 0.5);  //write the picture to the file [ImageData Writetofile:fullpath Atomically:no];  //Upload picture delete + (void) Removeuploadimage: (UIImage *) image withname: (NSString *) imagename{Nsfilemanager *filemanager = [[[Nsfilemanager alloc] init]; Nserror *error;  //Get sandbox directory nsstring *fullpath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]; FullPath = [FullPath stringbyappendingpathcomponent:@ "Tmpimage"]; if (![ FileManager Fileexistsatpath:fullpath]) {[FileManager Removeitematpath:fullpath error:&error];}}  //Get stored picture + (NSString *) Fetchuploadimagepath: (NSString *) imagename{nsstring *fullpath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]; FullPath = [FullPath stringbyappendingpathcomponent:@ "Tmpimage"]; FullPath = [FullPath stringbyappendingpathcomponent:imagename]; return fullpath;  //Determine if the file exists + (NSString *) isfileexists: (NSString *) fullpath{Nsfilemanager *FileManager = [[Nsfilemanager alloc] init]; if ([FileManager Fileexistsatpath:fullpath]) {return fullpath;} return nil;  //Data storage//+ (void)  //Get directory of stored files + (NSString *) datapath{//Here first specify the image access path (default write to Application sandbox) Nsarray *paths = Nssearc Hpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);  //and file a filename nsstring *filepathderectory = [Paths objectatindex:0];   return filepathderectory;  //Get data for specified file + (Nsmutabledata *) Fromfilenamepathfetchlocaldata: (NSString *) filename{//Save data to specified file Nsfilemanager *filemanager = [[Nsfilemanager alloc] init]; if ([FileManager Fileexistsatpath:filename]) {NSData *data = [FileManager contentsatpath:filename]; return [data Mutablecopy]; }   return nil;  //Storing data to specified file + (BOOL) Storagedatatofile: (NSData *) data fileName: (NSString *) filename{//Save data to specified file Nsfilemanager *filemanager = [[Nsfilemanager alloc] init]; if ([FileManager createfileatpath:filename contents:data Attributes:nil]) {return YES} else{return NO; }  //delete file + (void) DeleteFile: (NSString *) filename{Nsfilemanager *filemanager = [[Nsfilemanager alloc] init]; Nserror *error; [FileManager removeitematpath:filename error:&error];  //Get today's Date + (NSString *) fetchtodaydate{nsdate *currentdate = [NSDate Date]; NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; [Dateformatter Setdatestyle:nsdateformattermediumstyle]; return [Dateformatter stringfromdate:currentdate];  //Get yesterday's Date + (NSString *) fetchyesterdaydate{nsdate *yesterdaydate = [NSDate datewithtimeintervalsincenow:-(24 * 6 0 * 60)]; NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; [Dateformatter Setdatestyle:nsdateformattermediumstyle]; return [Dateformatter stringfromdate:yesterdaydate];  //Get the day before yesterday + (NSString *) fetchyesterdaybeforedate{nsdate *yesterdaydate = [NSDate datewithtimeintervalsincenow: -(2 * (24 * 60 * 60))]; NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; [Dateformatter SETDATESTYLE:NSDAteformattermediumstyle]; return [Dateformatter stringfromdate:yesterdaydate];  //Get data to store files  //Create files  //Create Directory + (NSString *) CreateDirectory: (NSString *) directoryname{Nsfilemanager *f Ilemanager = [[Nsfilemanager alloc] init]; Nserror *error; if (![ FileManager Fileexistsatpath:directoryname]) {[FileManager createdirectoryatpath:directoryname Withintermediatedirectories:yes Attributes:nil error:&error]; if (Error = = nil) {return directoryname;} else{return DirectoryName}} else{return DirectoryName}} Delete file + (void) RemoveFile: (NSString *) filepath{nserror *error nsfilemanager   *filemanager = [[Nsfilemanager alloc ] init]; if ([FileManager Fileexistsatpath:filepath]) {[FileManager Removeitematpath:filepath error:&error];} if (Error) { NSLog (@ "error =%@", error); }  //delete directory + (void) removedirectory{Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *documentspath = [Paths objectatindex:0]; NsstRing *removedirectorypath = [Documentspath stringbyappendingpathcomponent:[self fetchyesterdaybeforedate]]; Nserror *error;   Nsfilemanager *filemanager = [[Nsfilemanager alloc] init]; if ([FileManager Fileexistsatpath:removedirectorypath]) {[FileManager Removeitematpath:removedirectorypath error: &error]; } if (Error) {NSLog (@ "error =%@", error);} @end StorageData.h  ///STORAGEDATA.H//XUNYI7////Created by David on 13-6-28. Copyright (c) 2013 David. All rights reserved.   #import <Foundation/Foundation.h>   @interface storagedata:nsobject< Nsurlconnectiondatadelegate, nsurlconnectiondelegate>   + (Nsmutabledata *) Remotefetchdata: (NSString *) Dataurl; + (Nsmutabledata *) Localfetchdata: (NSString *) Dataurl; + (void) Saveuploadimage: (UIImage *) image withname: (NSString *) imagename; + (NSString *) Uploadimage: (UIImage *) image withname: (NSString *) imagename; + (NSString *) Fetchuploadimagepath; + (NSString *) Fetchuploadimagepath: (NSString *)imagename; + (void) Removeuploadimage: (UIImage *) image withname: (NSString *) imagename; + (NSString *) isfileexists: (NSString *) FullPath; + (void) RemoveFile: (NSString *) FilePath; @end


There are imperfections where you want to correct and modify


Related Article

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.