重用Apple Sandbox安全范围内的书签 [英] Reusing Apple Sandbox security-scoped bookmarks

查看:60
本文介绍了重用Apple Sandbox安全范围内的书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个正在读取应用程序沙箱外部文件的应用程序.我需要用户的许可才能读取这些文件,并且通过使用户在 NSOpenPanel 中选择路径来使我具有安全性范围大的书签.

I am creating a application which is reading files outside the application sandbox. I need the user's permission to read these files and I have got the security-scoped bookmarks to work, by making the user pick the path in a NSOpenPanel.

我的问题是我不希望每次应用程序访问相同路径时用户都给我权限.我将书签保存在 NSApplicationSupportDirectory 文件夹中.

My problem is that I don't want the user to give me permission every time the application accesses the same path. I am saving the bookmarks in the NSApplicationSupportDirectory folder.

这很好,直到用户重新启动计算机,然后看来已保存的书签才变得无效.从磁盘加载书签数据后,书签数据看起来完全正常,但是,即使应用程序使用书签作为重启前可用的路径,该应用程序仍需要新的安全范围的书签来读取文件.

This works fine until the user restarts the computer, then it seems like the saved bookmarks become invalid. The bookmark data looks completely fine when loaded from the disk, but the application requires new security-scoped bookmarks to read the files even though the application is using a bookmark for the path which worked before the restart.

我正在创建这样的书签:

I am creating the bookmarks like this:

NSData * bookmark  = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&err];
NSURL* bookmarkUrl = [NSURL URLByResolvingBookmarkData:bookmark options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&isStale error:&err]

我正在检查是否可以通过以下方式访问书签:

I am checking if the bookmark is accessible with the following:

[url startAccessingSecurityScopedResource];
[[NSFileManager defaultManager] contentsOfDirectoryAtURL:url includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:&err]

if (err && err.code == NSFileReadNoPermissionError)
{
    NSLog(@"No URL access");
    res = NO;
}
else if(err)
{
    NSLog(@"URL validate unexpected error:%@", err);
    res = NO;
}

if (res)
{
    //Bookmark works!
}
[url stopAccessingSecurityScopedResource];

只要我不重新启动Mac,它就可以工作.我可以退出应用程序并重新启动它,我的书签仍然有效.但是,一旦Mac重新启动,即使我使用的是在重新启动之前起作用的已保存书签,我也会收到 NSFileReadNoPermissionError 错误.

This works as long as I don't restart the Mac. I can quit the application and start it again and my bookmarks still work. But as soon as the Mac is restarted I get the NSFileReadNoPermissionError error even though I am using the saved bookmark that worked before I restarted.

有没有一种方法可以创建在Mac重新启动后可以使用的书签?

Is there a way to create a bookmark that works when the Mac has been restarted?

推荐答案

事实证明,我未能在开始和停止调用之间取得平衡.作为 startAccessingSecurityScopedResource 的rel =" noreferrer>文档指出,这会使书签无效:

It turned out I had failed to have balance between start and stop invokations. As the documentation of startAccessingSecurityScopedResource states, this can make the booksmarks invalid:

警告::您必须平衡对 startAccessingSecurityScopedResource 方法的每次调用与对 stopAccessingSecurityScopedResource 方法的相应调用.如果在不再需要文件系统资源时无法放弃访问权限,则您的应用程序会泄漏内核资源.如果泄漏了足够多的内核资源,您的应用程序将无法再通过沙箱(例如通过Powerbox或安全范围的书签)向其沙箱添加文件系统位置,直到重新启动为止.

Warning: You must balance every call to the startAccessingSecurityScopedResource method with a corresponding call to the stopAccessingSecurityScopedResource method. If you fail to relinquish your access when you no longer need a file-system resource, your app leaks kernel resources. If sufficient kernel resources are leaked, your app loses its ability to add file-system locations to its sandbox, such as via Powerbox or security-scoped bookmarks, until relaunched.

我最终也将书签存储在 NSUserDefaults 中,而不是将文件存储在 NSApplicationSupportDirectory 文件夹中.

I also ended up storing the bookmarks in NSUserDefaults instead of a file in the NSApplicationSupportDirectory folder.

这篇关于重用Apple Sandbox安全范围内的书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆