-1

我想在我的应用程序中添加一个清理 iphone 中多余文件的功能。但是我找不到实现它的api。你能告诉我吗?谢谢!

对不起。我没有准确描述我的问题。我想清除磁盘上的多余文件。多余的文件是iphone设备中的所有临时文件。功能类似于iClean(https://itunes。 apple.com/cn/app/iclean-remove-clean-all-deleted/id661800424?l=en&mt=8 )。

4

1 回答 1

1

高层概念:

您可能想看看通过管理文件缓存(文件系统性能)来提高文件系统性能。一般来说,修改文件系统的次数越少越好。您也只能编辑您在自己的应用程序中生成的文件。更重要的是阅读文件系统编程指南

在指南中,您将找到可帮助您实现特定于特定用例的目标的示例代码(请参见下面的示例):

清单 2-4 枚举目录的内容

NSURL *directoryURL = <#An NSURL object that contains a reference to a directory#>;

NSArray *keys = [NSArray arrayWithObjects:
    NSURLIsDirectoryKey, NSURLIsPackageKey, NSURLLocalizedNameKey, nil];

NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager]
                                     enumeratorAtURL:directoryURL
                                     includingPropertiesForKeys:keys
                                     options:(NSDirectoryEnumerationSkipsPackageDescendants |
                                              NSDirectoryEnumerationSkipsHiddenFiles)
                                     errorHandler:^(NSURL *url, NSError *error) {
                                         // Handle the error.
                                         // Return YES if the enumeration should continue after the error.
                                         return <#YES or NO#>;
                                     }];

for (NSURL *url in enumerator) {

    // Error-checking is omitted for clarity.

    NSNumber *isDirectory = nil;
    [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL];

    if ([isDirectory boolValue]) {

        NSString *localizedName = nil;
        [url getResourceValue:&localizedName forKey:NSURLLocalizedNameKey error:NULL];

        NSNumber *isPackage = nil;
        [url getResourceValue:&isPackage forKey:NSURLIsPackageKey error:NULL];

        if ([isPackage boolValue]) {
            NSLog(@"Package at %@", localizedName);
        }
        else {
            NSLog(@"Directory at %@", localizedName);
        }
    }
}

或者更短的方式...

清单 2-6 一次检索目录中的项目列表

NSURL *url = <#A URL for a directory#>;
NSError *error = nil;
NSArray *properties = [NSArray arrayWithObjects: NSURLLocalizedNameKey,
                          NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey, nil];

NSArray *array = [[NSFileManager defaultManager]
                   contentsOfDirectoryAtURL:url
                   includingPropertiesForKeys:properties
                   options:(NSDirectoryEnumerationSkipsHiddenFiles)
                   error:&error];
if (array == nil) {
    // Handle the error
}

接下来做什么:

一旦您在数组中获得信息,您可以查看使用 NSPredicate 搜索特定项目并使用 NSFileManager 删除实际文件。组合可用 API 有许多排列方式,这就是为什么可能需要更多信息才能获得更完整的解决方案的原因。最好的答案可能是您从上面的文档开始并尝试一些 API,然后再提出更具体的问题。如果它满足您的即时需求,请考虑接受此答案。

于 2013-10-22T15:23:22.787 回答