Created
September 10, 2021 10:33
-
-
Save bbenetskyy/892c606d5f7a0f1c96d556b8c26a9ecf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Foundation; | |
using QuickLook; | |
using System; | |
using System.Threading.Tasks; | |
using Xamarin.Essentials; | |
namespace Test.iOS | |
{ | |
public sealed class PdfService | |
{ | |
// filePath = Xamarin.Essentials.FileSystem.CacheDirectory + FileName (file.pdf) | |
public Task OpenAsync(string fileName, string filePath) | |
{ | |
var currentController = Platform.GetCurrentUIViewController(); | |
var qLPreviewController = new QLPreviewController(); | |
var item = new PreviewItem(fileName, filePath); | |
qLPreviewController.DataSource = new PreviewControllerDataSource(item); | |
currentController.PresentViewController(qLPreviewController, true, null); | |
return Task.CompletedTask; | |
} | |
private sealed class PreviewItem : QLPreviewItem | |
{ | |
private string _fileName; | |
private string _filePath; | |
public PreviewItem(string fileName, string filePath) | |
{ | |
_fileName = fileName; | |
_filePath = filePath; | |
} | |
public override string ItemTitle => _fileName; | |
public override NSUrl ItemUrl => NSUrl.FromFilename(_filePath); | |
} | |
private sealed class PreviewControllerDataSource : QLPreviewControllerDataSource | |
{ | |
private QLPreviewItem _item; | |
public PreviewControllerDataSource(QLPreviewItem item) => _item = item; | |
public override nint PreviewItemCount(QLPreviewController controller) => 1; | |
public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index) => _item; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment