简体   繁体   English

如何获取目录os x中的文件列表?

[英]How to get a list of files in the directory os x?

I'm developing an application in Objective-C. 我正在用Objective-C开发应用程序。 The user selects a directory (using NSOpenPanel ) and then in the application displays a list of the files in this directory. 用户选择一个目录(使用NSOpenPanel ),然后在应用程序中显示此目录中文件的列表。 This is working without a problem. 这没有问题。

However, if the application is open anew and without selecting a directory through NSOpenPanel , just want to get a list of files in the selected directory in the past startup error occurs 但是,如果重新打开应用程序并且未通过NSOpenPanel选择目录,则只想获取过去发生的所选目录中文件的列表,就会发生启动错误

NSCocoaErrorDomain Code = 257. NSCocoaErrorDomain代码= 257。

I suspect that if the user selects the directory manually via NSOpenPanel , the system somehow remembers it and give read access to this directory and getting her files (using the method contentsOfDirectoryAtPath ), and if the user does not select via NSOpenPanel directory and tries to get a list of its files error is generated access. 我怀疑如果用户通过NSOpenPanel手动选择目录,则系统会以某种方式记住该目录并授予对该目录的读取访问权限并获取其文件(使用contentsOfDirectoryAtPath方法),并且如果用户未通过NSOpenPanel目录选择并尝试获取NSOpenPanel目录它的文件列表错误将生成访问权限。

How can that be in this case? 在这种情况下怎么可能?

If your app is sandboxed and you want to have access to files/directories between runs, then you need to store a security-scoped bookmark to the directory, rather than the directory itself. 如果您的应用被沙盒化,并且希望在两次运行之间访问文件/目录,那么您需要将安全范围的书签存储到目录中,而不是目录本身。

This means that you need to add the boolean property com.apple.security.files.bookmarks.app-scope set to YES to the entitlements of the application, as well as com.apple.security.files.user-selected.read-write to allow file selection. 这意味着您需要向应用程序的权限中添加设置为YES的布尔属性com.apple.security.files.bookmarks.app-scope以及com.apple.security.files.user-selected.read-write以允许选择文件。

When you select the directory, you need to make a bookmark to it: 选择目录时,需要为其添加书签:

NSOpenPanel *panel = [[NSOpenPanel alloc] init];
panel.canChooseDirectories = YES;
NSInteger ret = [panel runModal];
if (ret == NSFileHandlingPanelOKButton) {
    NSURL *anUrl = [[panel URLs] lastObject];
    NSError *err;
    NSData *data = [anUrl bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
                   includingResourceValuesForKeys:nil
                                    relativeToURL:nil
                                            error:&err];
    if (!data) {
        NSLog(@"%@", err);
        return NO;
    }

Now that you've got the bookmark, you need to persist it eg by doing: 现在已经有了书签,您需要保留它,例如通过执行以下操作:

    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:data forKey:@"bookmark"];
    [def synchronize];

On restart of the application, you need to convert the bookmark from NSData back into an URL, this is done by doing: 重新启动应用程序时,需要将书签从NSData转换回URL,方法是:

BOOL stale = NO;
NSError *anError;
NSURL *url = [NSURL URLByResolvingBookmarkData:data
                                       options:NSURLBookmarkResolutionWithSecurityScope
                                 relativeToURL:nil
                           bookmarkDataIsStale:&stale
                                         error:&anError];
if (url && !stale) {
    [anURL startAccessingSecurityScopedResource];
    // Do something with URL
    [anURL stopAccessingSecurityScopedResource];
} else if (anError) {
    NSLog(@"%@", anError);
    return NO;
}

and if you had used the defaults persistence of the bookmark data, you would use: 如果使用了书签数据的默认持久性,则可以使用:

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSData *data = [def objectForKey:@"bookmark"];

to restore the bookmark data from the userdefaults. 从userdefaults恢复书签数据。

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

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