mirror of
https://github.com/bitwarden/mobile
synced 2026-01-07 11:03:54 +00:00
hybrid web view and duo html/js
This commit is contained in:
54
src/iOS/Controls/HybridWebViewRenderer.cs
Normal file
54
src/iOS/Controls/HybridWebViewRenderer.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System.IO;
|
||||
using Foundation;
|
||||
using WebKit;
|
||||
using Xamarin.Forms;
|
||||
using Xamarin.Forms.Platform.iOS;
|
||||
using Bit.App.Controls;
|
||||
using Bit.iOS.Controls;
|
||||
|
||||
[assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))]
|
||||
namespace Bit.iOS.Controls
|
||||
{
|
||||
public class HybridWebViewRenderer : ViewRenderer<HybridWebView, WKWebView>, IWKScriptMessageHandler
|
||||
{
|
||||
private const string JSFunction =
|
||||
"function invokeCSharpAction(data){window.webkit.messageHandlers.invokeAction.postMessage(data);}";
|
||||
|
||||
private WKUserContentController _userController;
|
||||
|
||||
protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
|
||||
{
|
||||
base.OnElementChanged(e);
|
||||
|
||||
if(Control == null)
|
||||
{
|
||||
_userController = new WKUserContentController();
|
||||
var script = new WKUserScript(new NSString(JSFunction), 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(e.OldElement != null)
|
||||
{
|
||||
_userController.RemoveAllUserScripts();
|
||||
_userController.RemoveScriptMessageHandler("invokeAction");
|
||||
var hybridWebView = e.OldElement as HybridWebView;
|
||||
hybridWebView.Cleanup();
|
||||
}
|
||||
|
||||
if(e.NewElement != null)
|
||||
{
|
||||
Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Uri, false)));
|
||||
}
|
||||
}
|
||||
|
||||
public void DidReceiveScriptMessage(WKUserContentController userContentController, WKScriptMessage message)
|
||||
{
|
||||
Element.InvokeAction(message.Body.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -217,6 +217,7 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Controls\ContentPageRenderer.cs" />
|
||||
<Compile Include="Controls\HybridWebViewRenderer.cs" />
|
||||
<Compile Include="Controls\ExtendedButtonRenderer.cs" />
|
||||
<Compile Include="Controls\CustomButtonRenderer.cs" />
|
||||
<Compile Include="Controls\CustomLabelRenderer.cs" />
|
||||
|
||||
Reference in New Issue
Block a user