Forum Xamarin.Mac


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How do I debug issue with NSSavePanel

ShammyLevvaShammyLevva Member ✭✭
edited January 2019 in Xamarin.Mac

Hi there, My app works fine in my environment and it works fine when running as a guest account. However it is getting rejected in the App Store review process because it throws the following error when the user tries to save and it should open a Save Panel....

Could not initialise an instance of the type AppKit.NSSavePanel the native init method returned nil. It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.

So a couple of issues.
1) Is this a bug in the Xamarin -> ObjC code rather than in my code?
2) Where do I set this condition so that the App passes the Appstore process?
2a) is setting this condition a good idea will that allow the save window to display?

I've been unable to replicate the issue even on a guest account so I'm struggling to understand what is going on.

    public static void Export(DataTable dt, string exportType)
        var dlg = new NSSavePanel
            Title = "Export data to Excel",
            AllowedFileTypes = new string[] { "csv" },
            Message = "Select location to export file to",
            NameFieldStringValue = exportType
        if (dlg.RunModal().Equals(NSModalResponse.OK))
            WriteFile(dt, dlg.Url.Path);
            UIHelpers.ShowMessage($"File written to {dlg.Url.Path}", "FTAnalyzer");

Best Answer


  • ShammyLevvaShammyLevva Member ✭✭

    === Visual Studio Community 2017 for Mac (Preview) ===

    Version 7.8 Preview (7.8 build 1471)
    Installation UUID: 5f9e4ddb-c2da-40c9-a9a0-4631f5e3f52f
    GTK+ 2.24.23 (Raleigh theme)
    Xamarin.Mac ( / 65ec520f)

    Package version: 516000221

    === Mono Framework MDK ===

    Mono (2018-06/b63e5378e38) (64-bit)
    Package version: 516000221

    === NuGet ===


    === .NET Core ===

    Runtime: /usr/local/share/dotnet/dotnet
    Runtime Version: 2.2.0
    SDK: /usr/local/share/dotnet/sdk/2.2.101/Sdks
    SDK Version: 2.2.101
    MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.16.0/lib/mono/msbuild/15.0/bin/Sdks

    === Xamarin.Profiler ===

    Version: 1.6.4
    Location: /Applications/Xamarin Profiler

    === Updater ===

    Version: 11

    === Apple Developer Tools ===

    Xcode 10.1 (14460.46)
    Build 10B61

    === Xamarin.Mac ===

    Version: (Visual Studio Community)
    Hash: 65ec520f
    Build date: 2018-12-05 12:06:09-0500

    === Xamarin.Android ===

    Version: (Visual Studio Community)
    Android SDK: /Users/alexander/Library/Developer/Xamarin/android-sdk-macosx
    Supported Android versions:
    7.1 (API level 25)

    SDK Tools Version: 26.1.1
    SDK Platform Tools Version: 26.0.2
    SDK Build Tools Version: 26.0.3

    === Microsoft Mobile OpenJDK ===

    Java SDK: /Users/alexander/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.9
    Android Designer EPL code available here:

    === Android Device Manager ===

    Hash: 06ceaea1

    === Xamarin Inspector ===

    Version: 1.4.3
    Hash: db27525
    Branch: 1.4-release
    Build date: Mon, 09 Jul 2018 21:20:18 GMT
    Client compatibility: 1

    === Xamarin.iOS ===

    Version: (Visual Studio Community)
    Hash: 65ec520f
    Branch: d15-9
    Build date: 2018-12-05 12:06:09-0500

    === Build Information ===

    Release ID: 708001471
    Git revision: 088a080b35e6b334bf6b67256bb93ebfc1ec0903
    Build date: 2018-12-17 18:14:14+00
    Build branch: release-7.8-p1
    Xamarin extensions: 6cffe6d190fbe788ac62840d29f6d0ff6956052d

    === Operating System ===

    Mac OS X 10.14.3
    Darwin 18.2.0 Darwin Kernel Version 18.2.0
    Fri Dec 14 18:43:36 PST 2018
    root:xnu-4903.240.10~4/RELEASE_X86_64 x86_64

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Hmm, I have not seen this before but the general error case it reasonable (when you think about it with ObjC colored glasses:

  • ShammyLevvaShammyLevva Member ✭✭
    edited January 2019

    Thanks for that very helpful. I'm getting an invalid cast though

    I changed my code to

          public static void Export(DataTable dt, string exportType)
                NSSavePanel dlg = NSSavePanelPatch.GetSavePanel();
                dlg.Title = "Export data to Excel";
                dlg.AllowedFileTypes = new string[] { "csv" };
                dlg.Message = "Select location to export file to";
                dlg.NameFieldStringValue = exportType;
                if (dlg.RunModal().Equals(NSModalResponse.OK))
                    WriteFile(dt, dlg.Url.Path);
                    ShowMessage($"File written to {dlg.Url.Path}", "FTAnalyzer");

    with the static as you wrote it. I added it as a subclass in my UIHelpers class.

            public static class NSSavePanelPatch
                [System.Runtime.InteropServices.DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
                extern static IntPtr IntPtr_objc_msgSend(IntPtr receiver, IntPtr selector);
                public static NSSavePanel GetSavePanel()
                    return ObjCRuntime.Runtime.GetNSObject<NSSavePanel>(IntPtr_objc_msgSend(ObjCRuntime.Class.GetHandle("NSSavePanel"), ObjCRuntime.Selector.GetHandle("savePanel")));

    and get error...

    System.InvalidCastException: Specified cast is not valid.
      at at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
      at ObjCRuntime.Runtime.ConstructNSObject[T] (System.IntPtr ptr, System.Type type, ObjCRuntime.Runtime+MissingCtorResolution missingCtorResolution, System.IntPtr selector, System.IntPtr method) [0x00048] in /Library/Frameworks/Xamarin.Mac.framework/Versions/
      at ObjCRuntime.Runtime.GetNSObject[T] (System.IntPtr ptr) [0x00103] in /Library/Frameworks/Xamarin.Mac.framework/Versions/
      at FTAnalyzer.UIHelpers+NSSavePanelPatch.GetSavePanel () [0x00001] in /Users/alexander/Projects/FTAnalyzer/FTAnalyzer/Utilities/UIHelpers.cs:44
      at FTAnalyzer.Utilities.ExportToExcel.Export (System.Data.DataTable dt, System.String exportType) [0x00001] in /Users/alexander/Projects/FTAnalyzer.Shared/FTAnalyzer.Shared/Exports/ExportToExcel.cs:42
      at FTAnalyzer.AppDelegate.ExportIndividuals (Foundation.NSObject sender) [0x00035] in /Users/alexander/Projects/FTAnalyzer/FTAnalyzer/AppDelegate.cs:135
  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    I'm able to reproduce that, I did not correctly have sandboxing setup. Let me get back to you...

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai
  • ShammyLevvaShammyLevva Member ✭✭

    Excellent many thanks I'll submit this to Apple and should go smooth now.

Sign In or Register to comment.