Prepare a MAUI environment

After my experience starting a new project and still feel the pain, here I show you How to prepare a MAUI environment for your projects.

Configure macOS and iOS

Here the basic steps we follow in order to deploy an application using macOS to deploy to iPhone and iPad. As you know, you must have a macOS machine to build and deploy your applications to real devices or use the simulator.

macOS

The first step is the check on macOS is Xcode is installed. If not, you have to install from the Apple Store. When you run Xcode, you should have a screenshot like the following.

Xcode splash screen - Prepare a MAUI environment
Xcode splash screen

Add an Apple account

The first action to do is to register your Apple account in Xcode and Download Manual Profiles.

Register an Apple account in Xcode - Prepare a MAUI environment
Register an Apple account in Xcode

Configure your devices

Now, from the menu, choose Window and then Devices and Simulators.

Xcode Devices and Simulators - Prepare a MAUI environment
Xcode Devices and Simulators

In order to deploy an application on a real device, you have to register your device in Xcode from here. As you can see in the following screenshot, I want to pair my new iPhone.

Pairing my iPhone to Xcode
Pairing my iPhone to Xcode

When the registration is completed and your device pairs with your macOS computer, you should see something like in the following screenshot. If it is your first registration, you won’t have any apps attached.

Paired devices in Xcode - Prepare a MAUI environment
Paired devices in Xcode

Dummy test

Now, the next step is create a dummy project to test is we can deploy on a real device. Choose from the first splash screen, the option Create a new Xcode project.

Create a new dummy project in XCode
Create a new dummy project in Xcode

Then, select App from the list and press Next. If you try to run the application on your real device, you can get this error:

The operation couldn’t be completed. Unable to launch com.puresourcecode.dummy because it has an invalid code signature, inadequate entitlements or its profile has not been explicitly trusted by the user.

Xcode
Xcode couldn't run the dummy application
Xcode couldn’t run the dummy application

So, this is normal because you are not a trusted developers (basically you haven’t pay Apple for a certificate). For this reason, we have to move to the physical devices to accept yourself as a trusted developer.

Note: you can do the next steps only if your device is registered in Xcode. If not, you won’t see the options for the Developer mode.

iPhone

Now, after the registration of your device in Xcode, you have to accept yourself as a trusted developer but before that you have to enable your device for the Developer Mode.

Enable Developer Mode

For that, go to the Settings of your device and look for Privacy and Security. If your device is connected to the macOS computer, you have the Developer Mode option. So, switch it ON.

Developer Mode iPhone
Developer Mode iPhone

Trust yourself

Finally, you can trust yourself! Go the the Settings and then General. Here, you have the option VPN & Device Management. Click on that.

iOS Settings - Prepare a MAUI environment
iOS Settings > General

So, here you can see all the profiles that are installed in your device. Plus, a new entry for the Apple Development. This option is available only after the first failed deployment with Xcode (for this reason I created the dummy project earlier).

iOS Settings > General > VPN & Device Management - Prepare a MAUI environment
iOS Settings > General > VPN & Device Management

In the screenshot, you can see that my profile Apple Development: enrico.rossini@me.com (obviously you will have your account) under Developer App is not accepted yet. So, click on it and you see a new page where you can accept to trust yourself as a developer.

Trust yourself - Prepare a MAUI environment
Trust yourself

So, now you can use your device to test your application for Xcode.

iPad

So, if you want to add your iPad, the process is exactly the same. Just put then screenshot for future reference.

First, go the the Privacy & Settings and enable the Developer Model after the registration with your macOS computer.

iPad Developer Mode
iPad Developer Mode

Now, if you try to deploy and run the dummy project, you get the error on your macOS computer and also on your iPad. You see the error in the screenshot below.

iPad couldn't trust the developer
iPad couldn’t trust the developer

So, go to General and then VPN & Device Management and click on the Apple Development under Developer App.

iPad VPN & Device management
iPad VPN & Device management

Finally, you can click on Trust Apple Deverloper.

iPad doesn't trust you as developer
iPad doesn’t trust you as developer

After clicking on that, you have to Trust the developer. For this reason, you see a popup window that asks you explicitly to click on Trust.

Accept to trust you as trusted developer
Accept to trust you as trusted developer

Finally, your device is ready to receive deployment from your macOS machine. If you want to deploy your application from Visual Studio to a real device, you have to pay Apple.

Configure Visual Studio

If you want to deploy from your Visual Studio to your physical devices, you have to be registered as a developer on the Apple Store Connect.

The error

In case you don’t have a registration with the Apple Store Connect, you will receive this error:

Error Automatic Provisioning is enabled but no Development Team was selected. Please select a team or switch to Manual Provisioning from the iOS Bundle Signing page.

Visual Studio

If you don’t pay Apple, you will only be able to deploy from Visual Studio to a iOS Simulator.

Generate keys from Apple

To generate keys, you must have an Admin account in App Store Connect. You may generate multiple API keys with any roles you choose.

To generate an API key to use with the App Store Connect API, log in to App Store Connect.

  1. Select Users and Access, and then select the API Keys tab.
  2. Click Generate API Key or the Add (+) button.
  3. Enter a name for the key. The name is for your reference only and is not part of the key itself.
  4. Under Access, select the role for the key.
  5. Click Generate.

The new key’s name, key ID, a download link, and other information appears on the page.

Download and Store the Private Key

Once you’ve generated your API key, you are given the opportunity to download the private half of the key. The private key is available for download a single time.

  1. Log in to App Store Connect.
  2. Select Users and Access, and then select the API Keys tab.
  3. Click “Download API Key” link next to the new API key.

The download link appears only if the private key has not yet been downloaded. Apple does not keep a copy of the private key.

Configure Android

Add Android SDK

When Visual Studio starts to compile the project, an alert comes up because we have to accept the Android licence and install the version 31 of the Android API. So, accept the licence first.

Android SDK - Licence Agreement - Exploring planets with MAUI
Android SDK – Licence Agreement

After that, choose from the list of Platforms at least the API Level 31 as Visual Studio required.

Android Platforms to install for MAUI - Exploring planets with MAUI
Android Platforms to install for MAUI

After the selection, you have to accept again the licence for the new platforms.

Accept the Android licences - Exploring planets with MAUI
Accept the Android licences

Now, compile again the project. It takes a while…

Half an hour later, Visual Studio installed all the dependencies for the project.

Now, in the Visual Studio toolbar, use the Debug Target drop down to select Android Emulators and then the Android Emulator entry

Add an Android Emulator
Add an Android Emulator

In the toolbar press the Android Emulator button

Start an Android Emulator
Start an Android Emulator

Accept again the licences 🤐and then in the User Account Control dialog, press the Yes button

User Account Control dialog asks for Android SDK Manager
User Account Control dialog asks for Android SDK Manager

Then, accept again the Android licences (I’m already bored to create application with MAUI) and then in the Visual Studio toolbar, press the Android Emulator button

Launch Android Emulator
Launch Android Emulator

Now, Visual Studio will start to create a default Android emulator. In the User Account Control dialog, press the Yes button

User Account Control dialog asks for Android Device Manager
User Account Control dialog asks for Android Device Manager

In the New Device window, press the Create button

Create a Default Android Device
Create a Default Android Device

Wait for Visual Studio to download, unzip, and create an Android emulator. After that, Close the Android Device Manager window

Android Device Manager with the Default emulator
Android Device Manager with the Default emulator

In the Visual Studio toolbar, press the Pixel 5 – API 30 (Android 11.0 – API 30) button to build and run the app

Visual Studio will start the Android emulator, build the app, and deploy the app to the emulator. After 10 minutes, the app is on the emulator, and I can click the button!

Wrap up

In conclusion, after all of it, we can finally deploy on a simulator, emulator or real device our application. If there is anything else I missed, please send me a message via the Forum.

Based on what we have done, I’m going to create a new project for display advertisements in our applications and monetize with apps.

But this is the subject of the next post! Happy coding!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.