.net-6.0mauiqlpreviewcontrolleruidocumentinteractioncontroller

MAUI .NullReferenceException (Native iOS QLPreviewController)


I am having a problem running after through the QLPreviewController interface.

view.PushViewController(previewController, true);

QLPreviewController starts up and displays what it should. At this point,

UIApplication.Main(args, null, typeof(AppDelegate));

throws an error


{System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Maui.Controls.Handlers.Compatibility.NavigationRenderer.SecondaryToolbar.LayoutToolbarItems(NFloat toolbarWidth, NFloat toolbarHeight, NFloat padding)
at Microsoft.Maui.Controls.Handlers.Compatibility.NavigationRenderer.SecondaryToolbar.LayoutSubviews()
at UIKit.UIApplication.UIApplicationMain(Int32 argc, String\[\] argv, IntPtr principalClassName, IntPtr delegateClassName)
at UIKit.UIApplication.Main(String\[\] args, Type principalClass, Type delegateClass)
at MAUISample.Program.Main(String\[\] args) in ...MAUISampleProject}

Does anyone know how to fix it or what I might be doing wrong ?

Greetings Matlas

I also tried to open the pdf file through UIDocumentInteractionController. The problem looks the same the file opens but the same error is thrown.


Solution

  • Referencing this question solved the issue for me for MAUI iOS .NET 8.0.1: How to find current UIViewController in Xamarin

    public async Task<string> SaveFileAsync(byte[] content, string localFilename)
    {
       string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
       string filePath = Path.Combine(documentsPath, localFilename);
       await File.WriteAllBytesAsync(filePath, content); // writes to local storage
       return filePath;
    }
    
    public void OpenPdf(string filePath)
    {
        string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        var PreviewController = UIDocumentInteractionController.FromUrl(NSUrl.FromFilename(filePath));
        var vc = GetTopViewController();
        PreviewController.Delegate = new PDFViewer(vc);
        PreviewController.PresentPreview(true);
    }
    
    public static UIViewController GetTopViewController()
    {
        var window = UIApplication.SharedApplication.KeyWindow;
        var vc = window.RootViewController;
        while (vc.PresentedViewController != null)
            vc = vc.PresentedViewController;
        if (vc is UINavigationController navController)
            vc = navController.ViewControllers.Last();
        return vc;
    }
    
    public class PDFViewer : UIDocumentInteractionControllerDelegate
    {
        UIViewController ownerVC;
    
        public PDFViewer(UIViewController vc)
        {
            ownerVC = vc;
        }
    
        public override UIViewController ViewControllerForPreview(UIDocumentInteractionController controller)
        {
            return ownerVC;
        }
    
        public override UIView ViewForPreview(UIDocumentInteractionController controller)
        {
            return ownerVC?.View ?? new UIView();
        }
    }