CommunityToolkit / Maui.NativeLibraryInterop

Maui.NativeLibraryInterop is a community-created library of binding samples to help .NET MAUI developers interop with native libraries more easily
MIT License
159 stars 24 forks source link

Update NativeLibraryInterop.BuildTasks to build bindings for macOS/tvOS #40

Open drasticactions opened 1 month ago

drasticactions commented 1 month ago

The underlying build tasks used in this repo for maintaining the XCFrameworks using by the bindings supports iOS and Mac Catalyst apps. That supports some app types, but the underlying libraries being bound to may also support additional platforms, like macOS (AppKit) and tvOS. XCFramework itself supports it, as well as the Mac/iOS SDK.

I wanted to add a binding to SDWebImage, so it could be more easily maintained by others, but it supports macOS and tvOS, and I don't want to try and PR support for only half the platforms. If we allow macOS and tvOS, that would open up more opportunities for bindings.

kbsanders commented 1 month ago

I am interested in this as well.

I need more AppKit features than a library targeting Mac Catalyst can provide. Is there any way to work around this limitation until macOS binding support can be added to the repo?

I did stumble across this article, where they use a Bundle/Plugin approach for Mac Catalyst + macOS/AppKit interop. https://betterprogramming.pub/how-to-access-the-appkit-api-from-mac-catalyst-apps-2184527020b5

Following the instructions in part B, I was able to successfully build their example project in Xcode/Swift, but I don't know enough yet to understand how to get the Bundle/Plugin bindings to work with C# and .NET MAUI.

That's when I discovered the Maui.NativeLibraryInterop repo but ran into trouble as soon as I tried to create bindings for a library targeting macOS:

MSBuild version 17.9.6+a4ecab324 for .NET
  Determining projects to restore...
  Restored /Users/kevin/Source/Maui.NativeLibraryInterop/template/macios/NewBinding.MaciOS.Binding/NewBinding.MaciOS.Binding.csproj (in 447 ms).  

/Users/kevin/Source/Maui.NativeLibraryInterop/template/sample/MauiSample.csproj :  
error NU1201: Project NewBinding.MaciOS.Binding is not compatible with net8.0-maccatalyst17.5 (.NETCoreApp,Version=v8.0).  
Project NewBinding.MaciOS.Binding supports: net8.0-macos14.5 (.NETCoreApp,Version=v8.0)  

/Users/kevin/Source/Maui.NativeLibraryInterop/template/sample/MauiSample.csproj :  
error NU1201: Project NewBinding.MaciOS.Binding is not compatible with net8.0-maccatalyst17.5 (.NETCoreApp,Version=v8.0) / maccatalyst-x64.  
Project NewBinding.MaciOS.Binding supports: net8.0-macos14.5 (.NETCoreApp,Version=v8.0)
drasticactions commented 1 month ago

So that's different from what I'm suggesting and what I think you want.

You're trying to bring in AppKit APIs into a .NET Mac Catalyst (net*.0-maccatalyst) app, for what I assume is a MAUI UI application. I'm requesting to be able to bind macOS Appkit libraries into a .NET macOS (net*.0-macos) app using these helper functions, so I could add bindings for more supported .NET SDK platforms (net8.0-tvos, net8.0-macos, etc) that are not used by the MAUI Cross Platform UI Framework.

Apple does not support bringing AppKit APIs into Catalyst except for ones it binds itself, which is why it can best be thought of as using private APIs. You can technically do it using Objective-C selectors and lots of reflection. But this is well beyond the scope of a library like this. This should be officially supported use cases provided by the platform, not hacks.