4

I tried to implement a CustomWebViewRenderer for iOS so I can append header to the request later on. I can see a border surrounded the web view but can't see the background which have the same color as the border. Only see a 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 any edit anything in the .plist file like Android for internet permission.

LittleFunny
  • 8,155
  • 15
  • 87
  • 198

1 Answers1

0

Your code works with https sites. To open http web site you need to add to info.plist the follow

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

You can limit an access for certain domains and customise more. See more here: Transport security has blocked a cleartext HTTP

Let us know if you need to load html from your resources. That will be different issue.

Yuri S
  • 5,355
  • 1
  • 15
  • 23