PDF in iOS WebView is greyed (in device simulator)

I have an application with local PDF file inside WebView like in standard example - Display a Local PDF File in a WebView
On Android everything is well, but on iOS screen is grey. When I touch the WebView I can see number of pages (so, it means that file is loaded).
Is it some kind of simulator bug? Was not able to test on real device yet.

Best Answer

  • programmerhammerprogrammerhammer BY ✭✭
    edited August 2017 Accepted Answer

    This problem was solved with replacing native control with WKWebView. Looks that article on official Xamarin site is outdated. Something like:

    if (Control == null)
            {
                var preferences = new WKPreferences();
                var config = new WKWebViewConfiguration();
                config.Preferences = preferences;
                WKWebView webView = new WKWebView(Frame, config);
                SetNativeControl(webView);
            }
    

    But now it is still question how to reload pdf in OnElementPropertyChanged event. This code doesn't reloading pdf:

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
    
            if (e.PropertyName == WebViewPDF.UriProperty.PropertyName)
            {
                var customWebView = Element as WebViewPDF;
                string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, string.Format("Content/{0}", WebUtility.UrlEncode(customWebView.Uri)));
                Control.LoadRequest(new NSUrlRequest(new NSUrl(fileName, false)));
                Control.Reload();
            }
        }
    

Answers

  • programmerhammerprogrammerhammer BYMember ✭✭
    edited August 2017 Accepted Answer

    This problem was solved with replacing native control with WKWebView. Looks that article on official Xamarin site is outdated. Something like:

    if (Control == null)
            {
                var preferences = new WKPreferences();
                var config = new WKWebViewConfiguration();
                config.Preferences = preferences;
                WKWebView webView = new WKWebView(Frame, config);
                SetNativeControl(webView);
            }
    

    But now it is still question how to reload pdf in OnElementPropertyChanged event. This code doesn't reloading pdf:

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
    
            if (e.PropertyName == WebViewPDF.UriProperty.PropertyName)
            {
                var customWebView = Element as WebViewPDF;
                string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, string.Format("Content/{0}", WebUtility.UrlEncode(customWebView.Uri)));
                Control.LoadRequest(new NSUrlRequest(new NSUrl(fileName, false)));
                Control.Reload();
            }
        }
    
  • Thanks for this.

    Xamarin staff this PDF tutorial page needs updating - https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/

    Also, currently the UWP PDF implementation is broken unless you force the function isSameOrigin() of pdf.js to always return true.

  • ShrutiBharadwajShrutiBharadwaj USMember ✭✭

    What did you do with the open question? My pdf is not loading..m getting a blank page

Sign In or Register to comment.