简体   繁体   English

从数据库中永久删除文件ios

[英]delete files from database permanently ios

I am Making an Audio recorder(m4a extension files). 我正在制作录音机(m4a扩展文件)。 I am Giving a particular URL for the output of the recorded File(in directory). 我正在为记录的File(在目录中)的输出提供特定的URL。 I am able to play it, save the path of the file in database and can retrieve it later. 我可以播放它,将文件的路径保存在数据库中,以后可以检索它。 EVery thing is going Fine. 很好,一切都很好。 BUT I am not able to delete the saved/unsaved files. 但是我无法删除已保存/未保存的文件。 Every time I record an audio , the file is taking a permanent space. 每次录制音频时,文件都会占用一个永久空间。 Am not able to delete them. 无法删除它们。

I tried it over internet(stackoverflow ofcourse). 我尝试通过互联网(课程的stackoverflow)。 I got Links like this: I have video URL and want to delete it from the iPhone using this URl 我得到了这样的链接: 我有视频URL,并想使用此URl从iPhone中删除它

But they are showing COCOA ERROR 4 when ever i try to delete them using codes like this: [[NSFileManager defaultManager] removeItemAtPath:strPath error:&error]; 但是,当我尝试使用以下代码删除它们时,它们会显示可可错误4 :[[NSFileManager defaultManager] removeItemAtPath:strPath error:&error];

Please suggest, and reply 请提出建议并回复

You typically accomplish this for resources you've saved in your Apps documents directory like this: 通常,您可以使用以下方法来完成此操作:

unlink([pathForURL UTF8String]);

where pathForURL is an NSString that describes the path to the resource you're deleting. 其中pathForURL是一个NSString,用于描述您要删除的资源的路径。

There may be case that file path which you provide is not correct. 有时您提供的文件路径不正确。 If its correct then try following with URL, it might solve your issue 如果正确,请尝试使用URL,这可能会解决您的问题

NSString *str= [outputFieldURL absoluteString];

    NSError *error;

    NSURL *url = [NSURL URLWithString:str];
    BOOL success = [[NSFileManager defaultManager] removeItemAtURL:url error:&error];


    if (!success) {
        NSLog(@"Error removing file at path: %@", error.localizedDescription);
    }
    else
    {
        NSLog(@"File removed  at path: %@", error.localizedDescription);
    }
}

Before deleting the file you have to check file there or not : 在删除文件之前,您必须在此处检查文件:

 NSFileManager* manager = [[NSFileManager alloc] init];
 if ([manager fileExistsAtPath:path]) {
    [manager removeItemAtPath:path error:&error];
 }

This is the path earlier i was getting , at which i was unable to write file 这是我之前获得的路径无法在其中写入文件

/var/mobile/Applications/8584F54E-75D2-4833-8826-29C125E53DBC/Library/Documentation/291013193758w.png /var/mobile/Applications/8584F54E-75D2-4833-8826-29C125E53DBC/Library/Documentation/291013193758w.png

This morning, I just run my code once again ,. 今天早上,我再次运行我的代码。 now its showing path 现在它的显示路径

/var/mobile/Applications/DDA14123-6A88-4756-B2E4-C4A3AA39AA5B/Documents/291013081335test.png /var/mobile/Applications/DDA14123-6A88-4756-B2E4-C4A3AA39AA5B/Documents/291013081335test.png

on this path am able to write my file 在这个路径上能够写我的文件

the Difference between two paths is that, first one is of Library/Documentation , where as second one is of Documents 两种路径之间的区别在于, 第一种是Library / Documentation第二种是Documents

dont know the difference, but it is working now 不知道区别,但现在可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM