Forum Xamarin.Forms

UIWebView Events not working anymore

C64GamerC64Gamer USMember ✭✭


i'm using the UIWebView object to create a PDF locally from a HTML String. This worked really well since the last update from Xamarin.Studio. I have no idea what happened. I had tested my project build with Xamarin.Forms on iOS 10.0 and 9.3. The old version 9.3 is not working too.

I'm using a dependency service in my Xamarin.Forms project to execute special code on each device.

//Generate PDF
DependencyService.Get<IHtmlToPdf>().SafeHTMLToPDF(html, "test");

html = My HTML as a string
"test" = The name of the file later. We get a test.pdf in this case

In the iOS method i use a delegate method to get access to the iOS WebView.

webView.Delegate = new WebViewCallBack(file);
webView.ScalesPageToFit = true;
webView.UserInteractionEnabled = false;
webView.BackgroundColor = UIColor.White;
webView.LoadHtmlString(html, null);

In my WebViewCallBack Class i'm "extends" from "UIWebViewDelegate". Now here is the problem. I'm overriding the event method :

public override void LoadingFinished(UIWebView webView)
            double height, width;
            int header, sidespace;

            width = 595.2;
            height = 841.8;
            header = 10;
            sidespace = 10;

            UIEdgeInsets pageMargins = new UIEdgeInsets(header, sidespace, header, sidespace);
            webView.ViewPrintFormatter.ContentInsets = pageMargins;

            UIPrintPageRenderer renderer = new UIPrintPageRenderer();
            renderer.AddPrintFormatter(webView.ViewPrintFormatter, 0);

            CGSize pageSize = new CGSize(width, height);
            CGRect printableRect = new CGRect(sidespace,
                              pageSize.Width - (sidespace * 2),
                              pageSize.Height - (header * 2));
            CGRect paperRect = new CGRect(0, 0, width, height);
            renderer.SetValueForKey(NSValue.FromObject(paperRect), (NSString)"paperRect");
            renderer.SetValueForKey(NSValue.FromObject(printableRect), (NSString)"printableRect");
            NSData file = PrintToPDFWithRenderer(renderer, paperRect);
            File.WriteAllBytes(filename, file.ToArray());

            Console.WriteLine("Filewriting complete...");

After the update the "LoadingFinished"-method is not called anymore. I have no idea why. Has someone an idea?


Sign In or Register to comment.