How to Perform OCR on iOS in .NET MAUI
.NET MAUI (Multi-platform App UI) is an evolution of the Xamarin.Forms framework, designed to create cross-platform apps for Android, iOS, macOS, and Windows using .NET. MAUI aims to simplify the process of building native user interfaces that can run on multiple platforms.
The IronOcr.iOS package brings OCR support to iOS!
How to use IronOCR on iOS in .NET MAUI
IronOCR iOS Package
The IronOcr.iOS package enables OCR features on iOS devices via .NET cross-platform projects. The vanilla IronOCR package is not needed.
Install-Package IronOcr.iOS
Install with NuGet
Install-Package IronOcr.iOS
Create a .NET MAUI Project
Under the Multiplatform section, select .NET MAUI App and continue.
Include the IronOCR.iOS Library
The library can be added in various ways. The easiest is perhaps by using NuGet.
- Inside Visual Studio, right-click on "Dependencies > Nuget" and select "Manage NuGet Packages ...".
- Select the "Browse" tab and search for "IronOcr.iOS".
- Select the "IronOcr.iOS" package and click on "Add Package".
To prevent issues with other platforms, modify the csproj
file to only include the package when targeting the iOS platform. In order to do so:
- Right-click on the *.csproj file for your project and select "Edit Project File".
Create a new
ItemGroup
element as such:<ItemGroup Condition="$(TargetFramework.Contains('ios')) == true"> <PackageReference Include="IronOcr.iOS" Version="YOUR_PACKAGE_VERSION" /> </ItemGroup>
<ItemGroup Condition="$(TargetFramework.Contains('ios')) == true"> <PackageReference Include="IronOcr.iOS" Version="YOUR_PACKAGE_VERSION" /> </ItemGroup>
XML- Move the "IronOcr.iOS" PackageReference inside the
ItemGroup
we just created.
The above steps will prevent the "IronOcr.iOS" package from being used on, e.g., Android platforms (for that purpose, install IronOcr.Android instead).
Edit "MainPage.xaml"
Edit the XAML file to display a button and a label to show the OCR result. For example:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MAUIIronOCRiOSSample.MainPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button
Text="Import File"
Clicked="ReadFileOnImport"
Grid.Row="0"
HorizontalOptions="Center"
Margin="20, 20, 20, 10"/>
<ScrollView
Grid.Row="1"
BackgroundColor="LightGray"
Padding="10"
Margin="10, 10, 10, 30">
<Label x:Name="OutputText"/>
</ScrollView>
</Grid>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MAUIIronOCRiOSSample.MainPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button
Text="Import File"
Clicked="ReadFileOnImport"
Grid.Row="0"
HorizontalOptions="Center"
Margin="20, 20, 20, 10"/>
<ScrollView
Grid.Row="1"
BackgroundColor="LightGray"
Padding="10"
Margin="10, 10, 10, 30">
<Label x:Name="OutputText"/>
</ScrollView>
</Grid>
</ContentPage>
Edit "MainPage.xaml.cs"
Start by instantiating the IronTesseract
object. Make sure to initialize IronTesseract
once in a class, as shown in the code below. Instantiating it directly in a method is not effective and could lead to unexpected errors. Then, use the FilePicker.PickAsync
method to select a file. From the FileResult
, open a stream for reading. Create a new OcrInput
object and use this object to load the image. Use the tesseract instance to perform OCR on the image and return the text. Finally, display the resulting text in a label.
The current implementation is limited to image files only. The package does not work with PDF documents yet. With this in mind, any configuration related to PDF documents should remain deactivated.
using System;
using IronOcr;
using Microsoft.Maui.Controls;
namespace MAUIIronOCRiOSSample;
public partial class MainPage : ContentPage
{
// Initialize IronTesseract once in a class
private IronTesseract ocrTesseract = new IronTesseract();
public MainPage()
{
InitializeComponent();
// Apply license key
IronOcr.License.LicenseKey = "IRONOCR-MYLICENSE-KEY-1EF01";
}
private async void ReadFileOnImport(object sender, EventArgs e)
{
try
{
var options = new PickOptions
{
PickerTitle = "Please select a file"
};
var result = await FilePicker.PickAsync(options);
if (result != null)
{
using var stream = await result.OpenReadAsync();
// Instantiate OcrInput
using var ocrInput = new OcrInput();
// Load image stream
ocrInput.LoadImage(stream);
// Perform OCR
var ocrResult = ocrTesseract.Read(ocrInput);
OutputText.Text = ocrResult.Text;
}
}
catch (Exception ex)
{
// Handle exceptions
System.Diagnostics.Debug.WriteLine(ex);
}
}
}
using System;
using IronOcr;
using Microsoft.Maui.Controls;
namespace MAUIIronOCRiOSSample;
public partial class MainPage : ContentPage
{
// Initialize IronTesseract once in a class
private IronTesseract ocrTesseract = new IronTesseract();
public MainPage()
{
InitializeComponent();
// Apply license key
IronOcr.License.LicenseKey = "IRONOCR-MYLICENSE-KEY-1EF01";
}
private async void ReadFileOnImport(object sender, EventArgs e)
{
try
{
var options = new PickOptions
{
PickerTitle = "Please select a file"
};
var result = await FilePicker.PickAsync(options);
if (result != null)
{
using var stream = await result.OpenReadAsync();
// Instantiate OcrInput
using var ocrInput = new OcrInput();
// Load image stream
ocrInput.LoadImage(stream);
// Perform OCR
var ocrResult = ocrTesseract.Read(ocrInput);
OutputText.Text = ocrResult.Text;
}
}
catch (Exception ex)
{
// Handle exceptions
System.Diagnostics.Debug.WriteLine(ex);
}
}
}
Lastly, switch the build target to iOS Simulator and run the project.
Run the Project
This will show you how to run the project and perform OCR.

Download .NET MAUI App Project
You can download the complete code for this guide. It comes as a zipped file that you can open in Visual Studio as a .NET MAUI App project.
Click here to download the project.
Using IronOcr.iOS in Avalonia
Setting up IronOcr.iOS in Avalonia is similar to MAUI, with one crucial difference: in addition to the latest .NET SDK version, you also need .NET SDK 8.0.101 installed to run IronOcr.iOS successfully. Afterward, IronOcr.iOS can be used in an Avalonia project with the same setup as described above.
If you want to perform OCR on Android, navigate to the following article to learn more: "How to Perform OCR on Android in .NET MAUI"
Frequently Asked Questions
What is .NET MAUI?
.NET MAUI (Multi-platform App UI) is an evolution of the Xamarin.Forms framework, designed to create cross-platform apps for Android, iOS, macOS, and Windows using .NET. It simplifies the process of building native user interfaces that can run on multiple platforms.
How do I add IronOCR.iOS to my .NET MAUI project?
You can add the IronOCR.iOS library to your .NET MAUI project using NuGet. In Visual Studio, right-click on 'Dependencies > Nuget' and select 'Manage NuGet Packages'. Then, search for 'IronOcr.iOS' and click 'Add Package'.
How can I configure the IronOCR.iOS package for iOS only?
To configure the IronOCR.iOS package for iOS only, modify the csproj file to include the package only when targeting the iOS platform. This involves creating a new ItemGroup with the condition to check for 'ios' in the target framework and moving the PackageReference inside this ItemGroup.
How do I perform OCR on an image in a .NET MAUI project?
In a .NET MAUI project, you can perform OCR by using the IronTesseract object. Use FilePicker.PickAsync to select an image file, load it into an OcrInput object, and then use IronTesseract to read the image and extract text.
Can IronOCR.iOS handle PDF documents?
Currently, IronOCR.iOS is limited to image files only and does not support PDF documents. Configuration related to PDF documents should remain deactivated.
What is required to use IronOCR.iOS in an Avalonia project?
To use IronOCR.iOS in an Avalonia project, you need the latest .NET SDK version and also .NET SDK 8.0.101 installed. The setup is similar to MAUI, with this additional SDK requirement.
Where can I find a sample .NET MAUI project using IronOCR.iOS?
You can download a sample .NET MAUI project using IronOCR.iOS from the provided link in the guide on the Iron Software website. It comes as a zipped file that you can open in Visual Studio.