Add custom Headers to every WebView call
Gordon S
501
Reputation points
I need to add an Authorization header to WebView and have it stay when the Url changes (because the page I load then redirects to my API location and I need the Auth token to be passed).
I have got it working for Android with:
[assembly: ExportRenderer(typeof(OAuthWebView), typeof(OAuthWebViewRenderer))]
namespace MyApp.Droid.Custom
{
public class OAuthWebViewRenderer : WebViewRenderer
{
public readonly Context LocalContext;
public OAuthWebViewRenderer(Context context) : base(context)
{
LocalContext = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
Android.Webkit.WebView.SetWebContentsDebuggingEnabled(true);
if (e.NewElement != null)
{
var authCode = ((OAuthWebView) Element).AuthCode;
var headers = new Dictionary<string, string>
{
["Authorization"] = $"Bearer {authCode}"
};
var webView = Control;
if (Control == null) {
webView = new Android.Webkit.WebView(LocalContext);
SetNativeControl(webView);
}
webView.Settings.JavaScriptEnabled = true;
webView.SetWebViewClient(new OAuthWebViewClient(headers));
if (Element.Source is UrlWebViewSource source)
{
webView.LoadUrl(source.ToString(), headers);
}
}
}
}
public class OAuthWebViewClient : WebViewClient
{
private readonly Dictionary<string, string> _headers;
public OAuthWebViewClient(Dictionary<string, string> headers)
{
_headers = headers;
}
[TargetApi(Value = 24)] // Nougat
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
{
if (request.Url == null) return false;
return HandleUri(view, request.Url.ToString());
}
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, string url)
{
return HandleUri(view, url);
}
private bool HandleUri(Android.Webkit.WebView view, string url)
{
view.LoadUrl(url, _headers);
return true;
}
}
}
However, I can't get the iOS version to work, mainly because all of the examples I've come across seem to be for the old WebView (UIWebView?)
[assembly: ExportRenderer(typeof(OAuthWebView), typeof(OAuthWebViewRenderer))]
namespace MyApp.iOS.Custom
{
public class OAuthWebViewRenderer : WkWebViewRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
this.ContentMode = UIViewContentMode.ScaleToFill;
}
var authCode = ((OAuthWebView) Element).AuthCode;
var webView = e.NewElement as OAuthWebView;
if (webView == null) return;
if (webView.Source == null) return;
var url = ((UrlWebViewSource)webView.Source).Url;
var newRequest = new NSMutableUrlRequest();
NSMutableDictionary dic = new NSMutableDictionary();
dic.Add(new NSString("Authorization"), new NSString("Bearer " + authCode));
newRequest.Headers = dic;
newRequest.Url = new NSUrl(url);
LoadRequest(newRequest);
}
}
}
The iOS code works, in that the WebView displays the first page as expected, however the Authorization header is not retained for when the transfer to my API endpoint happens (so I get "not authorized" error)
Developer technologies .NET Xamarin
5,380 questions
Sign in to answer