0

I am using QuickLook to preview Images, Pdf and Microsoft office documents. It is working fine to preview documents but its ShouldOpenUrl delegate method not firing whenever i try to open link from documents. Following is the code that i tried.

I test my app with iPhone and iPad having iOS v11.

// Open documents using title and file url
public void OpenDocument(string title, string url)
{
    var rootViewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
    var previewViewController = new QLPreviewController();
    previewViewController.DataSource = new DocumentPreviewDataSource(title, url);

    previewViewController.Delegate = new PreviewControllerDelegate();

    rootViewController.PresentViewController(previewViewController, true, null);
}

// QLPreviewControllerDelegate Implementation
public class PreviewControllerDelegate : QLPreviewControllerDelegate
{
    public override bool ShouldOpenUrl(QLPreviewController controller, NSUrl url, IQLPreviewItem item)
    {
        Console.WriteLine("PreviewControllerDelegate::ShouldOpenUrl: {0}", url.AbsoluteString);
        return true;
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Aamir Anwar
  • 93
  • 1
  • 10

1 Answers1

0

You can use the weakdelegate

public partial class xxxViewController : UIViewController,IQLPreviewControllerDelegate,IQLPreviewControllerDataSource
//. . . 

in method OpenDocument

public void OpenDocument()
{
  var previewViewController = new QLPreviewController();

  previewViewController.View.Frame = View.Bounds;

  previewViewController.WeakDelegate = this;

  previewViewController.WeakDataSource = this;

  this.PresentViewController(previewViewController, true,null);

}

And override the method in QLPreviewControllerDelegate and QLPreviewControllerDataSource

public nint PreviewItemCount(QLPreviewController controller)
{
  return 1;
}

public IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
{
  return new NSUrl("your url");
}

[Export("previewController:shouldOpenURL:forPreviewItem:")]
public bool ShouldOpenUrl(QLPreviewController controller, NSUrl url, IQLPreviewItem item)
{
    Console.WriteLine("PreviewControllerDelegate::ShouldOpenUrl: {0}", url.AbsoluteString);
    return true;
}

[Export("previewControllerWillDismiss:")]
public void WillDismiss(QLPreviewController controller)
{
   // do some thing
}

I use the above code and it works fine.

Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22