【问题标题】:Security-Scoped Bookmarks for a directory目录的安全范围书签
【发布时间】:2016-12-27 08:14:31
【问题描述】:

我需要为应用程序将某些文件写入该目录的目录授予完整的读/写权限。我读到使用沙盒应用程序需要Enable Security-Scoped Bookmark and URL Access 在重新启动应用程序后访问文件夹。

所以我试图根据这里的代码来实现它,并稍作修改What is the correct way to handle stale NSURL bookmarks?

     NSOpenPanel* openDlg = [NSOpenPanel openPanel];
    [openDlg setCanChooseDirectories:YES];
    [openDlg setCanCreateDirectories:YES];
    [openDlg setAllowsMultipleSelection:FALSE];
    if ( [openDlg runModal] == NSOKButton )
    {
        NSArray *files = [openDlg URLs];

        NSString* dirPath =[[files objectAtIndex:0] path];// absoluteString];
        BOOL isDirectory;
        NSFileManager* manager = [NSFileManager defaultManager];



        NSString *Dir = [dirPath stringByAppendingPathComponent:@"ScreenCaptures"];
        if (![manager fileExistsAtPath:Dir isDirectory:&isDirectory] || !isDirectory)
        {
            NSError *error = nil;

            [manager createDirectoryAtPath:Dir
               withIntermediateDirectories:NO
                                attributes:nil
                                     error:&error];
            if (error)
                NSLog(@"Error creating directory snap path: %@", [error localizedDescription]);

        }


            NSURL *url = [NSURL URLWithString:[Dir stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

            NSData *bookmark = nil;
            NSError *error = nil;
            bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
                     includingResourceValuesForKeys:nil
                                      relativeToURL:nil // Make it app-scoped
                                              error:&error];
            if (error) {
                NSLog(@"Error creating bookmark for URL (%@): %@", url, error);
                [NSApp presentError:error];
            }

            NSLog(@"bookmark: %@", bookmark);
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:bookmark forKey:@"bookmark"];

    }

但是上面的代码给了我错误

016-08-20 02:19:53.390 FileAccess[635:85753] modalSession has been exited prematurely - check for a reentrant call to endModalSession:
2016-08-20 02:19:59.979 FileAccess[635:85753] Error creating bookmark for URL (/Users/development/Documents/c/ScreenCaptures): Error Domain=NSCocoaErrorDomain Code=262 "Scoped bookmarks can only be made with file URLs" UserInfo={NSURL=/Users/development/Documents/c/ScreenCaptures, NSDebugDescription=Scoped bookmarks can only be made with file URLs}
2016-08-20 02:20:00.021 FileAccess[635:85753] CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme
2016-08-20 02:20:04.967 FileAccess[635:85753] bookmark: (null)

可能是什么问题?,上面的代码有什么问题。

【问题讨论】:

    标签: objective-c macos nsurl appstore-sandbox nsopenpanel


    【解决方案1】:

    您的第二条错误消息告诉您出了什么问题 - 您没有使用 file:// URL。

    这可以通过从您的路径变量正确创建 URL 来解决,但是您可能会更好地始终坚持使用 URL,而不是执行 URL -> 路径 -> URL 转换。您使用路径的所有操作都可以直接使用 URL 完成,只需查看 NSFileManager 和 NSURL 的文档即可。唯一可能不明显的是使用NSURL 的checkResourceIsReachableAndReturnError: 而不是NSFileManager 的fileExistsAtPath:,但是请仔细阅读checkResourceIsReachableAndReturnError: 的文档并听取其建议。

    进行这些更改应至少解决您报告的三个错误。

    HTH

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      相关资源
      最近更新 更多