Problem opening PDF that is saved locally dynamically on IOS #17796
Unanswered
weguimaraess
asked this question in
Q&A
Replies: 1 comment
-
Did you god any fixes for this? I am also stuck in this .... this works perfectly in the iOS simulator but not working in the physical device. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In the MAUI project I am implementing a kind of library in which I have a grid with a list of PDFs that are stored locally.
I have in my code:
`using Microsoft.Maui.Controls;
using System;
using System.IO;
#if IOS || MACCATALYST
using Microsoft.Maui.Handlers;
using WebKit;
#endif
namespace EuroAtlanticFoot
{
public partial class WebViewPage : ContentPage
{
public WebViewPage(string fileUrl)
{
InitializeComponent();
ConfigureCustomWebView();
LoadPdfViewer(fileUrl);
#if IOS || MACCATALYST
WebViewHandler.Mapper.AppendToMapping(nameof(IWebView), (handler, view) =>
{
if (handler.PlatformView is WKWebView wkWebView)
{
wkWebView.Configuration.Preferences.SetValueForKey(
Foundation.NSObject.FromObject(true),
new Foundation.NSString("allowFileAccessFromFileURLs")
);
#endif
}
#if IOS || MACCATALYST
var viewerPath = Foundation.NSBundle.MainBundle.PathForResource("viewer", "html", "wwwroot/pdfjs/web");
if (string.IsNullOrEmpty(viewerPath) || string.IsNullOrEmpty(fileUrl))
{
throw new InvalidOperationException("The file URL or viewer path is null or empty.");
}
var escapedPdfUrl = Uri.EscapeDataString(fileUrl);
var fullPdfJsUrl = $"file://{viewerPath}?file=file://{escapedPdfUrl}"; //even if was fileUrl without escaped method, not work
pdfWebView.Source = new UrlWebViewSource { Url = fullPdfJsUrl };
#else
pdfWebView.Source = fileUrl;
#endif
}
}
}
`
My sample PDF is stored as a BundleResource and so if I do as I did for viewerPath it opens, but for any other PDF that I store locally in my app I have to download it and it is stored inside my folder:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
And I pass the PDF path through the fileUrl, it comes as something:
/var/mobile/Containers/Data/Application/D65C2807-69CB-411C-A3C3-BC9A7592A9D2/Documents/FOOTs/Manuals/nameOfPDF.pdf
Can you tell me what needs to be done so that the PDF coming from fileUrl is opened within the viewer?
It is worth highlighting that:
Beta Was this translation helpful? Give feedback.
All reactions