How to integrate .NET MAUI PDF Viewer with a native iOS application?
In this article, you will learn how to use the .NET MAUI PDF Viewer in a native iOS application to view PDF documents by following the step-by-step process outlined below:
Step 1:
Create a new .NET iOS application and install the Syncfusion.Maui.PdfViewer nuget package from nuget.org.
Step 2:
In the project file of the native application, add <UseMaui>
true <UseMaui>
to enable the .Net Maui support demonstrated as follows. i.e. It tells the build system that your project is a .NET MAUI application and should be treated accordingly.
<PropertyGroup>
<TargetFramework>net8.0-ios</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<UseMaui>true</UseMaui>
<ImplicitUsings>enable</ImplicitUsings>
<SupportedOSPlatformVersion>13.0</SupportedOSPlatformVersion>
</PropertyGroup>
Step 3:
To initialize .NET MAUI in a native app, create a MauiAppBuilder object and invoke UseMauiEmbedding () to enable embedding. Then, configure it to set up the SyncfusionCore component within your .NET native app project.
Then, create a MauiApp object by invoking the Build() method on the MauiAppBuilder object. In addition, a MauiContext object should be made from the MauiApp object. The MauiContext object will be used when converting .NET MAUI controls to native types.
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
// Create a new window instance based on the screen size
Window = new UIWindow(UIScreen.MainScreen.Bounds);
MauiAppBuilder builder = MauiApp.CreateBuilder();
builder.UseMauiEmbedding<Microsoft.Maui.Controls.Application>();
builder.ConfigureSyncfusionCore();
// Register the Window.builder.Services.Add(new
Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(UIWindow), Window));
MauiApp mauiApp = builder.Build();
_mauiContext =new MauiContext(mauiApp.Services);
….
}
Step 4:
Create a folder named Assets in your project, add the required PDF file to this folder, and set the PDF file’s build action to Embedded resource in its properties.
Step 5:
Initialize the SfPdfviewer and set its DocumentSource property to the stream read from the PDF document added in the Assets folder. Then convert the SfPdfViewer into a native UIView and add it to the app’s UI.
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
.....
.....
// Create the PDF Viewer
SfPdfViewer pdfViewer = new SfPdfViewer();
pdfViewer.Background = Colors.White;
// Load a PDF document
var assembly = Assembly.GetExecutingAssembly();
pdfViewer.DocumentSource =
this.GetType().Assembly.GetManifestResourceStream("PDFViewerNativeEmbeddingiOS.Assets.PDF_Succinctly.pdf");
// Convert the .NET MAUI control to an iOS View object
UIView mauiView = pdfViewer.ToPlatform(_mauiContext);
mauiView.Frame = Window!.Frame;
// Create UIViewController and add the PDF Viewer to its subviews
var vc = new UIViewController();
vc.View!.AddSubview(mauiView);
// Set the UIViewController as the root view controller
Window.RootViewController = vc;
// Make the window visible
Window.MakeKeyAndVisible();
return true;
}
Step 6:
In the solution, set the native iOS project as the start-up project. Build, deploy, and run the project.
We hope you enjoyed learning how to integrate .NET MAUI PDF Viewer in a native iOS application.
Refer to our .NET MAUI PDF Viewer’s feature tour page to learn about its other groundbreaking feature representations. You can also explore our .NET MAUI PDF Viewer Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore our .NET MAUI PDF Viewer and other .NET MAUI components.
Please let us know in the following comments if you have any queries or require clarifications. You can also contact us through our support forums, support ticket or feedback portal. We are always happy to assist you!