Tag: Matter

  • .NET MAUI, Matter and Swift

    I’ve recently been working with Matter a lot. Matter is a standard for smart home devices which can play together nicely across ecosystems. In .NET MAUI on iOS this means using the native MatterSupport and Matter frameworks which are part of the OS.

    The Matter framework is an Objective-C library, and you can use the standard approach of creating a binding library with Objective Sharpie to integrate with it. It can look quite messy when you look into the documentation as a lot of APIs got deprecated and renamed between iOS 16.1 and around iOS 17 when things settled down a bit.

    MatterSupport is an odd one. It’s quite a small framework, having an API which your app uses to kick off the device pairing process, and a type which you implement in an App Extension which interacts with the OS during the process. It’s a Swift library so you can’t interop with it in the same way. The traditional approach is to create a second swift library project, expose the API you want to use and mark it up with @objc to create an API exposed as Objective-C. You can then create a .NET binding library from this. It does work, but is quite messy.

    Around the time of .NET 9.0, Microsoft started adding runtime support for Swift into .NET, but there were no tools to create bindings. An experimental tool showed up in the .NET Runtime Labs (https://github.com/dotnet/runtimelab/tree/feature/swift-bindings) but this hasn’t been updated for over a year. It didn’t support creating bindings for system frameworks so was no use for MatterSupport.

    Recently, I came across Justin Wojciechowski’s Swift .NET Bindings project on GitHub and this looked very promising. Justin had taken the unfinished runtime labs tool, forked it, and ran with it, creating a really powerful tool. Not only that, but he has also published a large set of .NuGet packages built with the tool, wrapping a load of Apple’s and common third-party frameworks. When I saw that MatterSupport was not one of these, I set about using the tool to generate my own. Unfortunately I hit a roadblock and contacted Justin to explain what I was stuck on. He very quickly responded and was able to update the tool to support MatterSupport (and Matter, as his tool does one-shot generation of Objective-C bindings too) and added these to his packages. See the readme for details.

    Therefore if you need to integrate with a Swift framework from your .NET app on iOS, I’d recommend checking if Justin has already generated one, and if not, use the Swift .NET Bindings tool to create your own.