.Net MAUI is a cross-platform framework for creating native mobile and desktop app with c# and Xaml. In my previous article, we did warn welcome to Dotnet MAUI and shared information about the History of Xamarin.

MAUI as everybody already knows is a name for a new upgrade solution as a Multi-platform APP UI framework for building native cross-platform apps with .Net for android, iOS, macOS, and Windows. I am going to show how to create, build and debug the First MAUI application using Visual Studio 2022.

NET MAUI is the .NET Multi-platform App UI, a framework for

MAUI and other third-party framework teams started working on support for upgrading the app and old NuGet package to MAUI. MAUI will also provide you support for building the apps in different modern patterns and frameworks MVVM, MVU and RxUI.

.NET MAUI IDE


Microsoft provided 3 fantastic IDE Tools to create and develop apps with .NET MAUI and the happy news is MAUI is an open source.
  1. Visual Studio
  2. Visual Studio for Mac
  3. Visual Studio Code

Visual Studio 2022

On Windows or Mac machines you can use Visual Studio Code or Visual Studio/ VS for Mac for development. You will need the visual studio 2022 17.1.0 preview version. Microsoft released Visual studio 2022 version 17.0 in Nov month, but MUAI is still in preview so you can start to install visual studio 2022 17.1.0 preview version to create your first MUAI application.

Build your first .NET MAUI app

After installation success, Open the Visual Studio IDE

What .NET MAUI Means for Xamarin Developers

In this sample demo application, I am  going to show Android, iOS, and Windows app using MAUI, so Get started with “Create New Project”

What is .NET MAUI, and How Does it Differ from Xamarin?

On the new Project template, select project type as an” MAUI” and you will all the MAUI preview template, if you are not finding the MAUI template means, you have not installed the latest preview version so make sure you have installed the latest preview version 17.1 ++.

Microsoft Replaces Xamarin Toolkits with New .NET MAUI

In the Configure your new project window, name your project, choose your project location where you need to save, and click the Create button

The future of Xamarin: .NET MAUI

MAUI Solutions Structure

After clicking on Create, MAUI solutions default template loaded with all the related MAUI NuGet packages. There is a single project with different platform folders and resources.

Creating first .Net MAUI project

MainPage.XAML

The main page, whichever design, will support all other platforms as well, and also Main Page XAML build action modified in MAUI app.

Main Page.Xaml build action modified with MAUIXAML, make sure Xaml Build action is correct.

Right click on the XAML page >>Properties>>BuildAction of XAML Page to MauiXaml.

Creating new .NET 6 MAUI project

.NET Generic Host


The Worker Service templates, create a .NET Generic Host, HostBuilder. The Generic Host can be used with other types of .NET applications, such as Console apps.

Maui Program class enables apps to be initialized from a single location, and provides the ability to configure fonts, services, and third-party libraries.

iOS, Android, windows, and all the platform entry point calls a CreateMauiApp method of the static MauiProgram class that creates and returns a MauiApp, while lunch the application

.NET MAUI: How MAUI Will it impact on Xamarin Native applications?

Application Class

The App class derives from the Application class

Workload Repair failed: `dotnet workload repair - MAUI



Resources

The resources are a good improvement in MAUI. The AppIcon, image and font folder will be available under the MAUI project and it is just a single SVG, Looks like Maui will just automatically take care of generating all the different icon sizes for different devices.


How do I determine where the error is when I check MAUI compatibility with maui-check?How do I determine where the error is when I check MAUI compatibility with maui-check?

Run IOS App

Building MAUI iOS applications requires access to Apple's build tools, which only run on a Mac. Because of this, Visual Studio 2022 must connect to a network-accessible Mac to build iOS applications.

You must install the Xcode 13.1++ version on the Mac machine before connecting Visual Studio 2022 to the Windows machine.

Windows Configuration 

  1. Connect same wifi network
  2. Download and install Visual Studio 2022 with MAUI

Mac Configuration

  1. Connect same wifi network
  2. Install Xcode 13.1 ++
  3. On Network preference > switch on Remote login
Pair to Mac machine from windows

Pair to Mac for Xamarin.iOS Development

Program.cs

Program class file is the main entry file on IOS app and executes the main method of the application, here you can define your custom app delegate file and other configuration files

Appdelegate.cs

Each platform will call MauiProgram static class for initialize. On iOS will call as below

You can select iOS simulator and device and click Run icon for executing the application



The out as like below



Run Android App

The android application, MainApplication.cs will execute first, it will call the MauiProgram static class initially as like below
 
You can select android simulator or device as below, press F5 to run the application
 


The output like below



Run Windows App


The Windows application executes  App.xaml.cs file first, it will call the MauiProgram static class initially as like below

Select the device and run the Application



MAUI Windows device

Demo Video

I have shared a recorded demo video with two versions, English and Tamil. It will help you to understand more about creating, building, and debuging the First MAUI application.

English Demo Video



Tamil Demo Video 



Hope this article is very useful for you, If you have any questions/ feedback/ issues, please write in the comment box

Startup Tracing can be used with Visual Studio 2019 Version 16.2 or Visual Studio for Mac 2019 Version 8.2. 
You can get started with Startup Tracing by editing your Android project’s .csproj file and adding the following property inside your Release <PropertyGroup>:
This can also be in the Android Options in your project settings starting with Visual Studio 2019 16.3 Preview 2:Enable startup tracing with a line of code.
For more documentation on Startup Tracing, see our release notes on this topic. 

Faster Startup Times With Startup Tracing On Android



Android Performance and App Size Challenge

  1. Choose one of your existing apps to enable these features with.
  2. Create a new issue in this GitHub Repository and follow the instructions.
  3. Enable the various features, log your before and after results, then submit your issue!
  4. Bonus Points: Post your results on Twitter with the hashtag #XamarinChallenge.



Google announced all Android app should support 64-bit architectures from Aug 1, 2019. 64-bit CPUs deliver faster, richer experiences for your users. Google Play will continue to support 32-bit apps for now, but as of August 1, 2019 all new apps and updates that include native code must have 64-bit versions as well as 32-bit versions when publishing to Google Play. This ruling applies to apps written in native, Xamarin or apps with links to third-party native libraries.


Android application supports four ABIs that can be selected when building mobile app. You can Select which ABIs your application supports in the project android options properties, if no ABI is specified, “armeabi-v71” is used in release build and As of Xamarin.Android 9.2, armeabi is no longer supported.


  1. armeabi-v7a – ARM-based CPUs with hardware floating-point operations and multiple CPU (SMP) devices.
  2. arm64-v8a – CPUs based on the 64-bit ARMv8 architecture.
  3. x86 – CPUs that support the x86 (or IA-32) instruction set.
  4. x86_64 – CPUs that support the 64-bit x86 .
You can follow the below steps for select 64-bit android application in the Visual studio and Visual studio mac.
Visual Studio in Windows machine:
Step 1: You can open Visual Studio in the windows machine, right-click on android project and select Properties
Step 2: Select the Android Options tab and Click the Advanced button and, check the architectures that you want to support.
Visual Studio Mac:
You can open Visual Studio mac in the mac machine and open/ create new project Android project.
Step 1:  Right-click on Android project under the Solution Explorer and select Properties.
Step 2: Under the Android Options page check the Packaging properties section and Click the Advanced button and select the architectures that you want to support:
How to check if your device is 32-bit or 64-bit?
You can also check if your phone is 64-bit in android using the app Antutu Benchmark. While this is a benchmarking app, you can just use it to view your device information. Open Antutu Benchmark and click on “info”.
https://fossbytes.com/wp-content/uploads/2017/09/Screenshot_20170914-113209-min-244x420.png
Summary:
The android applications already support 64-bit, no need to do anything, If you haven’t yet, you can change the as per the above steps as soon as possible. I hope this article will help you. Please leave your feedback/query using the comments box, and if you like this article, please share it with your friends.


Introduction:
Google is considering the users privacy seriously, it has updated its Google Play Developer Policy which restricting SMS, CALL_LOG access. If your app does not require access to Call Log or SMS permissions, you must remove the permissions from your app's manifest.
For apps requesting access to the SMS or Call Log permissions, the intended and permitted uses include default SMS handling, default phone handling, or Assistant handling capability, you should only access Call Log or SMS permissions when your app falls within permitted uses and only to enable your app’s critical core functionality.
Android App Should read the SMS some specific needs like verify OTP, The Google Play Services has enabled Android SMS Retriever API allows you to access the SMS Messages and verify it via SMS without device permissions. 
Verification Message Format:
You need to follow as per Google Policy for construct the verification message and send to the user device, the message should be following format.
  • Be no longer than 140 bytes
  • Begin with the prefix <#>
  • One-time Verification code
  • Special 11-character hash for you app. That Hash can be generated by the Application. (will explain following steps)
Now we have very good understanding about Google new policy and message format. This article, I will explain the following steps, it will help you to integrate Android OTP verification code auto read using Xamarin Forms.
  • Create New Xamarin Forms Application.
  • Add Google Play Services Auth
  • Create UI Design 
  • Dependency Service
  • Messaging Center subscribe 
  • ListenToSms Dependency
  • Broadcast Receiver
  • Generate Application Hash Key Helper
  • Demo Application
Create New Xamarin Forms Application
In order to implement Auto read OTP message, let’s start creating a new Xamarin.Forms project using Visual Studio 2019 or VS mac. When accessing Visual Studio 2019 mac for the first time, you will come across a new interface for opening a creating the projects.

Open Visual Studio Mac >>Create New Project or select open recent application.


The available templates will appear on a mac like below. Select Xamarin.Forms application with different mobile platforms.




Add Google Play Service NuGet Package:
After click on Next Button, The Xamairn Project template will generate and load the solutions into the local system. We need to install Xamarin.GooglePlayServices.Auth nuget package to our Android project for SMS Retriever API.
 Right click on Android Project >> Add Nuget Package >> Select or Search “Xamarin.GooglePlayServices.Auth”>> Install the Latest version of Google play service.

Create UI Design:
Create simple UI Design with one Entry and Button Control using Xaml in Xamarin Forms library.
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ReadOTPXamarinAndroid" x:Class="ReadOTPXamarinAndroid.MainPage">
   <StackLayout Padding="15" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" BackgroundColor="White">
        <Entry  PlaceholderColor="Black" Placeholder="Enter OTP" x:Name="smSEntry"></Entry>
        <Button Text="Wait for sms"  Clicked="ImageButton_OnClicked"></Button>
    </StackLayout>
</ContentPage>

The Design Look like below 
Dependency Service
DependencyService allows apps to call into platform-specific functionality from shared code. This functionality enables Xamarin.Forms apps to do anything that a native app can do. We need to create a interface design will define how you interact with platform-specific functionality. Here, reading OTP will support only android Platform and IOS will support with Autofill so create DependencyService interface for Listen to sms retriever. Create new class and interface for CommonServices.
using Xamarin.Forms;

namespace ReadOTPXamarinAndroid
{
    public static class CommonServices
    
        public static void ListenToSmsRetriever()
        
            DependencyService.Get<IListenToSmsRetriever>()?.ListenToSmsRetriever();
        
    
    public interface IListenToSmsRetriever
    
        void ListenToSmsRetriever();
    

}

Messaging Center subscribe:
Xamarin.Forms MessagingCenter enables different components to communicate without having to know anything about each other besides a simple Message contract. The MessagingCenter is a static class with Subscribe and Send methods that are used throughout the solution.
Subscribe - Listen for messages with a certain signature and perform some action when they are received. Multiple subscribers can be listening for the same message.
Send - Publish a message for listeners to act upon. If no listeners have subscribed then the message is ignored.
Here, we are creating utility for subscribe message listener, it will help for if any new message recived the device notify to the solutions.
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace ReadOTPXamarinAndroid
{
   public static class Utilities
    
        private static readonly object cc = new object();
        public static void Subscribe<TArgs>(this object subscriber, Events eventSubscribed, Action<TArgs> callBack)
        
            MessagingCenter.Subscribe(subscriber, eventSubscribed.ToString(), new Action<object, TArgs>((e, a) => { callBack(a); }));
        
        public static void Notify<TArgs>(Events eventNotified, TArgs argument)
        
            MessagingCenter.Send(cc, eventNotified.ToString(), argument);
        

    
}
Create Enum for message Event type 
using System;
namespace ReadOTPXamarinAndroid
{
    public enum Events
    
        SmsRecieved,
    
}
Code Behind Design View:
The Code Behind add the Subscribe message listener and if any message receive from message app with specific format, it will get notify and read the meesage and assign the OTP value into the Entry Box.   
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace ReadOTPXamarinAndroid
{
    public partial class MainPage : ContentPage
    
        public MainPage()
        
            InitializeComponent();
            this.Subscribe<string>(Events.SmsRecieved, code =>
            
                smSEntry.Text = code;
            });
        
        private void ImageButton_OnClicked(object sender, EventArgs e)
        
            CommonServices.ListenToSmsRetriever();
        
    
}
ListenToSms Dependency:
Let start create dependency to android project ,if you are not added Xamarin.GooglePlayServices.Auth NuGet package, try to add before going to create Instance.
Get an instance of SmsRetrieverClient, used to start listening for a matching SMS message. 

SmsRetrieverClient client = SmsRetriever.GetClient(Application.Context);

Starts SmsRetriever, which waits for ONE matching SMS message until timeout (5 minutes). The matching SMS message will be sent via a Broadcast Intent .
            var task = client.StartSmsRetriever();
Listen for success/failure of the start Task. 
using Java.Lang;
using ReadOTPXamarinAndroid.Droid;
using Application = Android.App.Application;

[assembly: Dependency(typeof(ListenToSms))]

namespace ReadOTPXamarinAndroid.Droid
{
    public class ListenToSms : IListenToSmsRetriever
    
        public void ListenToSmsRetriever()
        
           
            SmsRetrieverClient client = SmsRetriever.GetClient(Application.Context);
            var task = client.StartSmsRetriever();
            task.AddOnSuccessListener(new SuccessListener());
            task.AddOnFailureListener(new FailureListener());
        
        private class SuccessListener : Object, IOnSuccessListener
        
            public void OnSuccess(Object result)
            
            
        
        private class FailureListener : Object, IOnFailureListener
        
            public void OnFailure(Exception e)
            
            
        
    

}
Broadcast Receiver:
The BroadcastReceiver that will be listening to the broadcasts of the above SmsRetreieverClient.SMS Retriever API has provided us with an intent filter SmsRetriever.SmsRetrievedAction which we will use to register our BroadcastReceiver, which we’re going name as SMSBroadcastReceiver, and implement as following.
using System.Linq;
using System.Text.RegularExpressions;
using Android.App;
using Android.Content;
using Android.Gms.Common.Apis;
using Com.Google.Android.Gms.Auth.Api.Phone;
using ReadOTPXamarinAndroid;

namespace ReadOTPXamarinAndroid.Droid
{
    [BroadcastReceiver(Enabled = true, Exported = true)]
    [IntentFilter(new[] { SmsRetriever.SmsRetrievedAction })]
    public class SmsReceiver : BroadcastReceiver
    
        private static readonly string[] OtpMessageBodyKeywordSet = { "DevEnvExe Generated OTP" }; //You must define your own Keywords
        public override void OnReceive(Context context, Intent intent)
        
            try
            

                if (intent.Action != SmsRetriever.SmsRetrievedAction) return;
                var bundle = intent.Extras;
                if (bundle == null) return;
                var status = (Statuses)bundle.Get(SmsRetriever.ExtraStatus);
                switch (status.StatusCode)
                
                    case CommonStatusCodes.Success:
                        var message = (string)bundle.Get(SmsRetriever.ExtraSmsMessage);
                        var foundKeyword = OtpMessageBodyKeywordSet.Any(k => message.Contains(k));
                        if (!foundKeyword) return;
                        var code = ExtractNumber(message);
                        Utilities.Notify(Events.SmsRecieved, code);
                        break;
                    case CommonStatusCodes.Timeout:
                        break;
                

            
            catch (System.Exception)
            
                // ignored
            
        
        private static string ExtractNumber(string text)
        
            if (string.IsNullOrEmpty(text)) return "";
            var number = Regex.Match(text, @"\d+").Value;
            return number;
        
    

}
We have done code for now; it will go for testing. Before going to demo let we re consider the format of SMS, in that we need to get know about Application Hash key, Ley you follow below step for generate application hash key  
Generate Application Hash Key Helper
The hash string is made of your app’s package name and your app’s public key certificate. To generate the hash code, just run the following C# Method to generate hash to be included in your SMS message.
You need to make sure you are generate hash key and append to the OTP message, Without the correct hash, your app won't receive the message callback. 
Hash key will generate once per app and stored. Then you can remove this helper class from your code and create new class into the native android project.

using System;
using System.Linq;
using System.Text;
using Android.Content;
using Android.Content.PM;
using Android.Util;
using Java.Security;
using Java.Util;

namespace ReadOTPXamarinAndroid.Droid.Helper
{
    public class AppHashKeyHelper
    
        private static string HASH_TYPE = "SHA-256";
        private static int NUM_HASHED_BYTES = 9;
        private static int NUM_BASE64_CHAR = 11;

        /// <summary>
        /// Retrieve the app signed package signature
        /// known as signed keystore file hex string
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private static string GetPackageSignature(Context context)
        
            PackageManager packageManager = context.PackageManager;
            var signatures = packageManager.GetPackageInfo(context.PackageName, PackageInfoFlags.Signatures).Signatures;
            return signatures.First().ToCharsString();
        

        /// <summary>
        /// Gets the app hash key.
        /// </summary>
        /// <returns>The app hash key.</returns>
        /// <param name="context">Android app Context.</param>
        public static string GetAppHashKey(Context context)
        
            string keystoreHexSignature = GetPackageSignature(context);

            String appInfo = context.PackageName + " " + keystoreHexSignature;
            try
            
                MessageDigest messageDigest = MessageDigest.GetInstance(HASH_TYPE);
                messageDigest.Update(Encoding.UTF8.GetBytes(appInfo));
                byte[] hashSignature = messageDigest.Digest();

                hashSignature = Arrays.CopyOfRange(hashSignature, 0, NUM_HASHED_BYTES);
                String base64Hash = Android.Util.Base64.EncodeToString(hashSignature, Base64Flags.NoPadding | Base64Flags.NoWrap);
                base64Hash = base64Hash.Substring(0, NUM_BASE64_CHAR);

                return base64Hash;
            
            catch (NoSuchAlgorithmException e)
            
                return null;
            
        
    
}
You can call the GetAppHashkey method into MainActivity and debug the solution and get the hash key value.
Demo and Download Source Code:
The Application Ready now and you can also download the source code from Github , start the application using Android Emulator and click Phone icon from extended controls and add the message as per follow format with hash key. 


Summary
In this article, you learned about Verify OTP Automatically in Android without SMS read permission using Xamarin Forms. I hope this article will help you. Please leave your feedback/query using the comments box, and if you like this article, please share it with your friends.

Featured Post

Improving C# Performance by Using AsSpan and Avoiding Substring

During development and everyday use, Substring is often the go-to choice for string manipulation. However, there are cases where Substring c...

MSDEVBUILD - English Channel

MSDEVBUILD - Tamil Channel

Popular Posts