2

This problem did not happen before macOS Ventura(Xcode 14.2), but I think the process of handling the security policy to Sandbox file access has changed significantly since this current version.

I have not changed any code, but when I run NSSavePanel() in compliance with the security policy in sandbox, as shown below, it returns "-2" and I can no longer access the folder properly.

string filePth = "/Users/hibara/Desktop/sample.jpg";
NSUrl urlPath = NSUrl.FromFilename(filePth);

NSError error;
var securityScopedUrl = urlPath.CreateBookmarkData(NSUrlBookmarkCreationOptions.WithSecurityScope, null, null, out error);
if (securityScopedUrl == null)
{
  Console.WriteLine($"Unable to create security scoped resource: {error.LocalizedDescription}");
  return;
}

var isStale = false;
var resolvedUrl = NSUrl.FromBookmarkData(securityScopedUrl, NSUrlBookmarkResolutionOptions.WithSecurityScope, null, out isStale, out error);
if (!resolvedUrl.StartAccessingSecurityScopedResource())
{
  Console.WriteLine($"Unable to start accessing security scoped resource: {error.LocalizedDescription}");
  return;
}

var savePanel = new NSSavePanel
{
  //DirectoryUrl = resolvedUrl,
  Title = "Save your file",
  NameFieldStringValue = "Untitled.txt"
};

int ret = (int)savePanel.RunModal();
if (ret == 1)
{
  var saveUrl = savePanel.Url;
  // save file to saveUrl
}
else if (ret == 0)
{
  Console.WriteLine("User canceled the operation.");
}
else if ( ret == -2)
{
  //The -2 returns.
  return;
}

resolvedUrl.StopAccessingSecurityScopedResource();

Absolutely no NSSavePanel is displayed and "-2" is the return value.

How can we successfully display NSSavePnel while complying with Apple's security policy for file access? Please help me!

hibara
  • 137
  • 11

0 Answers0