Xamarin: CustomWebView form on iOS did not display a simple web page

I tried to implement CustomWebViewRenderer for iOS to add a header to the request later. I see that the border surrounded the web view, but cannot see the background that is the same color as the border. Look only at the white blank space inside the border.

public class CustomWebViewRenderer : ViewRenderer<CustomWebView, UIWebView>
{
    protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
    {
        base.OnElementChanged(e);

        var control = new UIWebView();
        control.Layer.BorderColor = new CoreGraphics.CGColor(1.0f, 1.0f, 0.0f);
        control.Layer.BorderWidth = 10;
        control.Layer.BackgroundColor = new CoreGraphics.CGColor(1.0f, 1.0f, 0.0f);
        control.Delegate = new WebViewDelegate(control, Element.Token); 
        SetNativeControl(control);

        if (!String.IsNullOrEmpty(Element.Token) && !String.IsNullOrEmpty(Element.Url))
        {
            Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Url)));
        }
    }

    protected void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        var element = ((CustomWebView)Element);

        if (e.PropertyName.Equals(nameof(element.Token)))
        {
            ((WebViewDelegate)Control.Delegate).Token = element.Token;

            Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Url)));
        }
    }

    class WebViewDelegate : UIWebViewDelegate
    {
        public string Token { get; set; }

        private UIWebView _view;
        public WebViewDelegate(UIWebView view, string token)
        {
            _view = view;
            Token = token;
        }
        public override bool ShouldStartLoad(UIWebView webView,
                                              NSUrlRequest request,
                                              UIWebViewNavigationType navigationType)
        {
            //request.Headers.SetValueForKey((NSString)"Authorization", (NSString)("Bearer " + Token));

            return true;
        }

Do I need to edit anything in a .plist file, for example Android to access the Internet.

+4
source share
1 answer

Your code works with https sites. To open an http site, you need to add the following to info.plist

<key>NSAppTransportSecurity</key> 
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

. . : HTTP-

, html . .

0

Source: https://habr.com/ru/post/1671209/


All Articles