Resize and Screen.Orientation event from WKWebView Xamarin not working in Angular

Viewed 219

I have a Xamarin mobile app who include a WebView. This one display an Angular Website. When I change my mobile orientation, our Angular code doesn't catch this event on iOS (WKWebView). But I don't have any troubles with the Android WebView, Resize and screen orientation events are catched. Then I supposed my problem is on the iOS Xamarin part.

Angular window resize event catch :

    @HostListener('window:resize', ['$event'])
resizeMap() {
    this.mapWidth = window.innerWidth;
    this.mapHeight = window.innerHeight;
    this.changeDetectorRef.detectChanges();
    this.mapService.map.updateSize();
}

Angular screen orientation event catch

screen.orientation.addEventListener('change', () => {
            const { type } = screen.orientation;
            if (!type.startsWith('portrait')) {
                Swal.fire(
                    this.l('Information'),
                    this.l('LandscapeOrientationNotRecommended'),
                    'info'
                );
                // alert(this.l('LandscapeOrientationNotRecommended'));
            }
        });

Custom Xamarin iOS WKWebView

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName == "Source")
        {
            if (Element.Source != null)
            {
                Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Source)));
            }
        }
    }
    protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
    {
        base.OnElementChanged(e);

        this.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
        this.ContentMode = UIViewContentMode.ScaleToFill;

        if (e.OldElement != null)
        {
            userController.RemoveAllUserScripts();
            userController.RemoveScriptMessageHandler("invokeAction");
            var hybridWebView = e.OldElement as CustomWebView;
            hybridWebView.Cleanup();
        }
        if (e.NewElement != null)
        {
            if (Control == null)
            {
                userController = new WKUserContentController();
                var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false);
                userController.AddUserScript(script);
                userController.AddScriptMessageHandler(this, "invokeAction");

                var config = new WKWebViewConfiguration { UserContentController = userController };
                var webView = new WKWebView(Frame, config);
                SetNativeControl(webView);
            }
            if (Element.Source != null)
            {
                string contentDirectoryPath = Path.Combine(NSBundle.MainBundle.BundlePath);
                Control.LoadHtmlString(Element.Source, new NSUrl(contentDirectoryPath, true));
            }
        }
    }


    public void DidReceiveScriptMessage(WKUserContentController userContentController, WKScriptMessage message)
    {
        if (message.Body != null)
        {
            Element.InvokeAction(message.Body.ToString());
        }
    }
}

}

If someone have any suggestions ?

Thanks for your help.

0 Answers
Related