PDF is not displayed in iOS 13 and above

I am using the following code to display the PDF document in Xamarin.Forms iOS and it works fine before updating the Xcode version 11.2, after the update the PDF is not displayed when using the following code.

public class PreviewControllerDS : QLPreviewControllerDataSource { private QLPreviewItem _item; public PreviewControllerDS(QLPreviewItem item) { _item = item; } public override nint PreviewItemCount(QLPreviewController controller) { return 1; } public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index) { return _item; } } public class QLPreviewItemFileSystem : QLPreviewItem { string _fileName, _filePath; public QLPreviewItemFileSystem(string fileName, string filePath) { _fileName = fileName; _filePath = filePath; } public override string ItemTitle { get { return _fileName; } } public override NSUrl ItemUrl { get { return NSUrl.FromFilename(_filePath); } } } public class QLPreviewItemBundle : QLPreviewItem { string _fileName, _filePath; public QLPreviewItemBundle(string fileName, string filePath) { _fileName = fileName; _filePath = filePath; } public override string ItemTitle { get { return _fileName; } } public override NSUrl ItemUrl { get { var documents = NSBundle.MainBundle.BundlePath; var lib = Path.Combine(documents, _filePath); var url = NSUrl.FromFilename(lib); return url; } } }

And the final code for displaying the PDF file.

`
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string filePath = Path.Combine(path, filename);

FileStream fileStream = File.Open(filePath, FileMode.Create);
stream.Position = 0;
stream.CopyTo(fileStream);
fileStream.Flush();
fileStream.Close();

QLPreviewController qlPreview = new QLPreviewController();
QLPreviewItem item = new QLPreviewItemBundle(filename, filePath);
qlPreview.DataSource = new PreviewControllerDS(item);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(qlPreview, true, null);
`

Does anyone have any idea why the above code is not working?

Sign In or Register to comment.