Open pdf file using Xamarin.Essentials.Launcher.OpenAsync() on iphone

Wei Wen 1,126 Reputation points
2021-11-10T15:59:38.09+00:00

On Android, it will display the document. But on iOS, it only downloads the file. In info.plist, I added LSSupportsOpeningDocumentsInPlace and Supports Document Browser. But this does not change anything. How can I have pdf document open on iOS? Do I need to use a web view? Preferably, I would like word document and even text file to also be able to open.

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2021-11-11T03:21:26.573+00:00

    Welcome to our Microsoft Q&A platform!

    Xamarin.Essentials.Launcher enables an application to open a URI by the system, it will not open a pdf file, you could open it by Word if your device have installed Microsoft Word app, you need to specify LSApplicationQueriesSchemes in your Info.plist file. scheme name of Microsoft Word is ms-word, you can even open other apps if you have set the scheme name, refer to https://learn.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/mobile/scenarios/from-app
    https://learn.microsoft.com/en-us/xamarin/essentials/launcher?tabs=ios#additional-platform-setup

    You could also use a web view to display PDF, refer to te following code:
    XAML

    <WebView x:Name="MyWebview"> </WebView>  
    

    CodeBehind

    public MainPage()  
            {  
                InitializeComponent();  
    
            string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);  
            string filePath = Path.Combine(documentsPath, "a.pdf");  
           // MyWebview.Source = filePath;  
           MyWebview.Source = "https://www.xxxxxxx.pdf";//you coud load url or load local file path  
        }  
    

    In addition, you can disply PDF by UIDocumentInteractionController or QuickLook.framework or PDFKit in iOS, refer to https://stackoverflow.com/questions/66326804/i-have-written-a-code-for-previewing-a-pdf-file-in-xamarin-ios-now-how-can-i-an
    https://learn.microsoft.com/en-us/samples/xamarin/ios-samples/ios11-pdfannotationwidgetsadvanced/

    I also write a demo by using Custom Renderers and PDFKit, you could have a try.

    Create a custom view to display

    public class SamplePDFView:View  
        {          
        }  
    

    XAML

    <customview:SamplePDFView></customview:SamplePDFView>  
    

    ViewRenderer

    public class SamplePDFViewRenderer: ViewRenderer<SamplePDFView, PdfView>  
        {  
            private PdfView pdfView;  
        protected override void OnElementChanged(ElementChangedEventArgs<SamplePDFView> e)  
        {              
            if (pdfView==null)  
            {  
                var pdfFilePath = NSBundle.MainBundle.PathForResource("a", "pdf");// you could change this path, my file is in  Resources folder  
    
                  
               var pdfData =  NSData.FromFile(pdfFilePath);  
               //save pdf  
                //string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);  
                //byte[] dataBytes = pdfData.ToArray();  
                //File.WriteAllBytes(Path.Combine(documentsPath, "a.pdf"), dataBytes);  
                //InvokeOnMainThread(() => {  
                //    new UIAlertView("Done", "File downloaded and saved", null, "OK", null).Show();  
                //});  
    
                var doc = new PdfDocument(pdfData);  
                //var doc = new PdfDocument(new NSUrl("https://www.xxxxxxxxxxx.pdf")); load url  
                pdfView = new PdfView();  
    
                pdfView.Document = doc;  
            }  
    
            SetNativeControl(pdfView);  
        }  
    

    ----------------Update(Download the pdf and display)------------------
    XAML

     <StackLayout>  
            <Button Text="click to download" Clicked="Button_Clicked" BackgroundColor="Red"></Button>  
            <WebView x:Name="MyWebview" WidthRequest="300" HeightRequest="400" ></WebView>  
      </StackLayout>  
    

    Click the button to download and save to local Document path

    private void Button_Clicked(object sender, EventArgs arg)  
            {  
                var webClient = new WebClient();  
                webClient.DownloadProgressChanged += (s, e) => {  
                    Console.WriteLine("Download... {0}%", e.ProgressPercentage);   
                };  
                webClient.DownloadDataCompleted += (s, e) => {  
                    var data = e.Result;  
                    string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);  
                    string filePath = Path.Combine(documentsPath, "a.pdf");  
                    File.WriteAllBytes(filePath, data);  
                    Device.BeginInvokeOnMainThread(()=> {  
                        MyWebview.Source = filePath;  
                    });  
                };  
                var url = new Uri("https://knownow.blob.core.windows.net/otherfiles/myfourthprivategroup/alert/other/163ce70a-7878-4c74-9551-a94f3e1c7223_sample.pdf?sv=2020-08-04&st=2021-11-14T19%3A55%3A29Z&se=2021-11-17T19%3A55%3A29Z&sr=c&sp=r&sig=5vZUrXF9i68W9zibRL8oJjLwHIypy1zj40JS%2B83dgew%3D");  
                webClient.DownloadDataAsync(url);  
            }  
    

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.