Developing .NET MAUI Apps for macOS

This guide provides comprehensive information on building native .NET Multi-platform App UI (.NET MAUI) applications that run on macOS. .NET MAUI allows you to create cross-platform applications from a single codebase using C# and XAML, targeting Windows, macOS, Android, and iOS.

Prerequisites

To develop .NET MAUI apps for macOS, you need the following:

Setting Up Your Development Environment

Follow these steps to configure your Mac for .NET MAUI development:

  1. Install .NET SDK: If you haven't already, download and install the latest .NET SDK.
    dotnet workload install maui
  2. Install Xcode Command Line Tools: Open a terminal and run:
    xcode-select --install
    Follow the on-screen prompts to complete the installation.
  3. Configure Xcode: Open Xcode, go to Xcode > Preferences > Locations, and ensure that the Command Line Tools are set to your installed Xcode version.
Tip: For the best experience, ensure you are using the latest stable versions of all tools. Updates often include performance improvements and bug fixes.

Creating Your First MAUI macOS App

You can create a new .NET MAUI project using the .NET CLI or Visual Studio for Mac.

Using the .NET CLI

Open your terminal, navigate to the directory where you want to create your project, and run:

dotnet new maui -n MyMauiMacApp
cd MyMauiMacApp
dotnet build -f net6.0-maccatalyst
dotnet run -f net6.0-maccatalyst

This command creates a new MAUI project named MyMauiMacApp, builds it for the Mac Catalyst platform, and then runs it.

Using Visual Studio for Mac

  1. Launch Visual Studio for Mac.
  2. Click File > New Solution....
  3. In the template selector, navigate to .NET MAUI and choose .NET MAUI App.
  4. Click Continue, enter your project name (e.g., MyMauiMacAppVS), and click Create.
  5. Once the project is created, select Mac Catalyst as the target framework from the dropdown in the toolbar.
  6. Click the Run button (the green play icon).

Understanding Mac Catalyst

.NET MAUI apps for macOS are built using the Mac Catalyst technology. Mac Catalyst allows you to bring your iPad apps to Mac. .NET MAUI leverages this by providing a unified API that abstracts away the platform-specific details, enabling you to write code once and deploy it across multiple platforms, including macOS via Mac Catalyst.

Key Concepts

Common Tasks for macOS Development

App Icon

To set your app icon for macOS:

  1. Add your icon assets to the Resources/AppIcon folder in your .NET MAUI project.
  2. Ensure the icons are in the required sizes (e.g., 16x16, 32x32, 128x128, 256x256, 512x512).
  3. Configure your .csproj file to reference these icons. Visual Studio for Mac usually handles this automatically.

Window Management

.NET MAUI provides APIs to control the application window on macOS:

// Example: Setting window properties
Microsoft.Maui.Controls.Application.Current.Windows[0].Width = 800;
Microsoft.Maui.Controls.Application.Current.Windows[0].Height = 600;

Platform-Specific Features

For features unique to macOS, you might need to use platform-specific APIs:

You can access platform-specific APIs using conditional compilation or by checking the platform at runtime.

#if MACCATALYST
    // macOS specific code here
    var macWindow = Microsoft.Maui.Controls.Application.Current.Windows[0].Handler.PlatformView as UIKit.UIWindow;
    // ... access native macOS APIs
#endif

Debugging and Deployment

Debugging

Visual Studio for Mac and Visual Studio (on Windows, targeting Mac Catalyst) provide excellent debugging capabilities. You can set breakpoints, inspect variables, and step through your code.

Deployment

To deploy your .NET MAUI app to macOS:

  1. Ad Hoc Distribution: You can distribute your app to a limited number of Macs for testing.
  2. Mac App Store Distribution: To distribute your app on the Mac App Store, you'll need an Apple Developer Program membership and follow Apple's notarization and signing requirements.
Note: App distribution requires signing your application with appropriate certificates and provisioning profiles.

Troubleshooting Common Issues

For more advanced topics and detailed API references, please consult the official Microsoft Learn .NET MAUI documentation.