I am trying to delete a video from the document directory, but the video isn't deleting.
Here is how I am trying to delete the video:
//Delete Video
NSError *error = nil;
//NSData *videoData = [NSData dataWithContentsOfURL:self.finalURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:#"/vid1.mp4"];
[[NSFileManager defaultManager] removeItemAtPath: tempPath error: &error];
UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:#"Congratulation:" message:#"Successfully removed" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[removeSuccessFulAlert show];
You might get a good hint at your problem if you put in a line after your "removeItemAtPath" that says something like:
BOOL success = [[NSFileManager defaultManager] removeItemAtPath: tempPath error: &error];
if(!success)
{
NSLog(#"error from removing item at path %# is %#",
tempPath, [error localizedDescription]);
} else {
UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:#"Congratulation:" message:#"Successfully removed" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[removeSuccessFulAlert show];
}
Try this instead:
NSString *tempPath = [documentsDirectory stringByAppendingPathComponent:#"vid1.mp4"];
Related
I create a Bar button item (trash can) using storyboard.App crashes when i try to delete a file from document directory on button action. Here is code so far:
#property (retain, nonatomic) IBOutlet UIBarButtonItem *deleteCsv;
-(void)deleteCsv:(id)sender{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:fileDataString4];
NSError *error;
BOOL success = [fileManager removeItemAtPath:filePath error:&error];
if (success) {
UIAlertView *removedSuccessFullyAlert = [[UIAlertView alloc] initWithTitle:#"Congratulations:" message:#"Successfully removed" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[removedSuccessFullyAlert show];
}
else
{
NSLog(#"Could not delete file -:%# ",[error localizedDescription]);
}
I think you should check first whether file exist at path or not with following code,
if ([[NSFileManager defaultManager] fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:<your file path>]])
{
//Delete your file
}
By following this you will be able to handle the crash if it is occuring because of file nonexistence
i am downloading a pdf file from server,and i can see the file in my simulator.But i can't find in my device. I am saving the file into the document file. Below is my code,am sure my code is perfect,
-(void)downloadPdfBtnPressed:(id)sender
{
NSString *dowloadURL = [DownloadUrlArray objectAtIndex:[sender tag ]];
NSString *filename =[dowloadURL lastPathComponent];
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:#"/PDFDownload"];
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:nil];
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:dowloadURL]];
if(pdfData)
{
stringPath = [stringPath stringByAppendingPathComponent:filename];
[pdfData writeToFile:stringPath atomically:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"PDF"
message:#"PDF file is downloaded"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
I have a zip file of images on my backend server. I want to download the zip and unarchive the content (images) to NSArray. Is there a way to do it?
Thank you)
You could to use SSZipArchive in order to work with zip files.
Below line would unzip and assign images to a NSArray
// Unzipping
NSString *zipPath = #"path_to_your_zip_file";
NSString *destinationPath = #"path_to_the_folder_where_you_want_it_unzipped";
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
// Zipping
NSString *zippedPath = #"path_where_you_want_the_file_created";
NSArray *inputPaths = [NSArray arrayWithObjects:
[[NSBundle mainBundle] pathForResource:#"photo1" ofType:#"jpg"],
[[NSBundle mainBundle] pathForResource:#"photo2" ofType:#"jpg"]
nil];
[SSZipArchive createZipFileAtPath:zippedPath withFilesAtPaths:inputPaths];
To delete a file:
- (void)deleteFile:(NSString *)fileName
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];
NSError *error;
BOOL success = [fileManager removeItemAtPath:filePath error:&error];
if (success) {
UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:#"Congratulation:" message:#"Successfully removed" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[removeSuccessFulAlert show];
}
else
{
NSLog(#"Could not delete file -:%# ",[error localizedDescription]);
}
}
I'm new to IOS development and i have a problem in downloading my attachments from mail.when i'm trying to download image or PDF attachments from my mail to documents directory using MailCore2 SDK it is downloading sucessfully.but my problem is that the downloaded image or PDF displays nothing (i.e.,).It gives me the file but with a empty file with zero bytes.im able to know that problem is with the NSData what im using to writeToFile is incorrect.But im unable to solve it.please help me.
Here is the code i'm using to download file from mail
MCOIMAPFetchContentOperation * op = [self.imapSession fetchMessageByUIDOperationWithFolder:#"INBOX" uid:[sender tag]];
[op start:^(NSError * error, NSData * data) {
if ([error code] != MCOErrorNone) {
return;
}
NSAssert(data != nil, #"data != nil");
MCOMessageParser * msg = [MCOMessageParser messageParserWithData:data];
if ([msg.attachments count] > 0)
{
MCOIMAPPart *part = [msg.attachments objectAtIndex:0];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSLog(#"%#",part.filename);
NSString *attachmentPath = [[saveDirectory stringByAppendingString:#"/Downloads"] stringByAppendingPathComponent:part.filename];
if (fileExists) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message"
message:#"Fail Existed in Download Path."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else{
[msg.data writeToFile:attachmentPath atomically:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message"
message:#"Download Success /n Saved in Downloads"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
[self.tableView reloadData];
}
}];
please help me to get out of this problem and tell me where am i doing mistake.
Thanks in advance.
I used like this:
MCOIMAPFetchContentOperation * op = [self.imapSession fetchMessageByUIDOperationWithFolder:#"INBOX" uid:[sender tag]];
[op start:^(NSError * error, NSData * data)
{
if ([error code] != MCOErrorNone)
{
return;
}
NSAssert(data != nil, #"data != nil");
MCOMessageParser * msg = [MCOMessageParser messageParserWithData:data];
if ([msg.attachments count] > 0)
{
MCOAttachment *attachment = [msg.attachments objectAtIndex:0];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSLog(#"%#",attachment.filename);
NSString *downLoadDirectory = [saveDirectory stringByAppendingString:#"/Downloads"];
if (![[NSFileManager defaultManager] fileExistsAtPath:downLoadDirectory])
{
[[NSFileManager defaultManager] createDirectoryAtPath:downLoadDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *attachmentPath = [downLoadDirectory stringByAppendingPathComponent:attachment.filename];
if ([[NSFileManager defaultManager] fileExistsAtPath:attachmentPath])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message"
message:#"Fail Existed in Download Path."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
BOOL res = [[attachment data] writeToFile:attachmentPath atomically:YES];
if (res)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message"
message:#"Download Success /n Saved in Downloads"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
}
}];
I hope they are useful.
when open sqlite file from mail it come to folder named inbox in my documents directory
and i use this code to copy this file to document directory
- (void) copyAdd
{
NSString *docsDir;
NSArray *dirPaths;
NSString *databasePath;
NSFileManager *filemgr = [NSFileManager defaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString* inboxPath = [docsDir stringByAppendingPathComponent:#"Inbox"];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: #"CAT.sqlite"]];
if(![filemgr fileExistsAtPath:databasePath]){
[filemgr copyItemAtPath:inboxPath toPath:databasePath error:nil];
NSLog( #"copy create database");
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"sucess" message:#"COPY CAT DB" delegate:self cancelButtonTitle:#"dismis" otherButtonTitles:nil, nil];
[alert show];
}else{
NSLog( #"Failed to open/create database");
}
it create folder with name CAT.sqlite and inside this folder i found the dataBase CAT.sqlite
how can i copy the file named CAT.sqlite from Inbox folder to document directory without create folder with the same name .
this is my code that solve the problem the only missing thing is to specify the file name that i need to copy from the inbox folder not the all folder but CAT.sqlite folder
- (void) copyAdd
{
NSString *docsDir;
NSArray *dirPaths;
NSString *databasePath;
NSFileManager *filemgr = [NSFileManager defaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString* inboxPath = [docsDir stringByAppendingPathComponent:#"Inbox"];
NSString *filePath = [inboxPath stringByAppendingPathComponent:#"CAT.sqlite"];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: #"CAT.sqlite"]];
if(![filemgr fileExistsAtPath:databasePath]){
// NSLog(#"dbPAth : %#",dbPAth);
[filemgr copyItemAtPath:filePath toPath:databasePath error:nil];
NSLog( #"copy create database");
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"sucess" message:#"COPY CAT DB" delegate:self cancelButtonTitle:#"dismis" otherButtonTitles:nil, nil];
[alert show];
}else{
NSLog( #"Failed to open/create database");
}
}