目录

时间:2016-08-20 06:54:45

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

我需要为应用程序将某个文件写入此目录的目录提供完全读/写权限。我读到使用沙盒应用程序,重新启动应用程序后,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)

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

1 个答案:

答案 0 :(得分:4)

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

这可以通过从路径变量中正确创建URL来解决,但是您可能最好坚持使用URL并且不使用URL - >路径 - >网址转换。您使用该路径的所有操作都可以直接使用网址完成,只需查看NSFileManagerNSURL的文档即可。唯一可能不明显的是使用NSURL' checkResourceIsReachableAndReturnError:而不是NSFileManager' fileExistsAtPath:,但请阅读checkResourceIsReachableAndReturnError:的文档{1}}仔细并听取其意见。

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

HTH