Skip to content

3. Installation for Maui

Jimmy Pun edited this page Feb 22, 2023 · 2 revisions

- Install package from nuget

  1. Install Nuget package to Forms, Android and iOS project, you may need to specify the version since it is still in pre-release mode.
Install-Package BarcodeScanner.Mobile.Maui
  1. Add the handles in MauiProgram.cs
using BarcodeScanner.Mobile;
 var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                })
                .ConfigureMauiHandlers(handlers =>
                {
                    // Add the handlers
                    handlers.AddBarcodeScannerHandler();
                });
            return builder.Build();
  1. Edit your maui.csproj
<PropertyGroup>
	<!-- Choose either one, if you only want to deploy in any one. 
             (Maui is still unstable, I am not sure if it is required... -->
	<!-- Android Config -->
	<TargetFrameworks>net6.0-android;</TargetFrameworks>
	<!-- iOS Config -->
	<!--<TargetFrameworks>net6.0-ios;</TargetFrameworks>-->
</PropertyGroup>

<!-- Setup iOS RuntimeIdentifier and Cpu target, support real device only -->
<PropertyGroup Condition="( '$(TargetFramework)' == 'net6.0-ios' )">
	<RuntimeIdentifier>ios-arm64</RuntimeIdentifier>
	<PlatformTarget>arm64</PlatformTarget>
</PropertyGroup>
		

<!-- Have to include this package here otherwise build will be failed -->
<ItemGroup Condition="(  '$(TargetFramework)' == 'net6.0-android' )">
	<PackageReference Include="Xamarin.Google.MLKit.BarcodeScanning" Version="116.1.2.5">
	</PackageReference>
</ItemGroup>

- Android setup

  1. Manifest.xml
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />
<!-- In case you need the features of Scan from Image, add below permissions -->

<application ...............>
........
	<provider android:name="androidx.core.content.FileProvider" android:authorities="{your_app_id}.fileprovider" android:exported="false" android:grantUriPermissions="true">
		<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
	</provider>
........
</application>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  1. In case you need the features of Scan from Image, add file_paths.xml with Build Action "AndroidResource" to Resources\xml (You may create "xml" folder if it is not exist)
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
	<external-files-path name="my_images" path="Pictures" />
	<external-files-path name="my_movies" path="Movies" />
</paths>

- iOS Setup

  1. Edit Info.plist, add camera rights
	<key>NSCameraUsageDescription</key>
	<string>Require to use camera</string>
	<!-- In case you need the features of Scan from Image, add below permissions -->
	<key>NSPhotoLibraryUsageDescription</key>
	<string>This app needs access to photos.</string>
	<key>NSPhotoLibraryAddUsageDescription</key>
	<string>This app needs access to the photo gallery.</string>
Clone this wiki locally