Saturday, March 30, 2024
HomeiOS Developmentjava - ios maui.internet customized webview default BaseUrl path

java – ios maui.internet customized webview default BaseUrl path


i’ve this practice web-view as comply with in :
MainPage.xaml

                   ```
       <controls:HybridWebView
x:Identify="MyWebView"
HeightRequest="140"
HorizontalOptions="Fill"
Supply="{Binding Supply}"
VerticalOptions="FillAndExpand"
WidthRequest="512" />

and the custoum webview code is:

    HybridWebView.cs



 public class SourceChangedEventArgs : EventArgs
 {
     public WebViewSource Supply
     {
         get;
         personal set;
     }

     public SourceChangedEventArgs(WebViewSource supply)
     {
         Supply = supply;
     }
 }

 public class JavaScriptActionEventArgs : EventArgs
 {
     public string Payload { get; personal set; }

     public JavaScriptActionEventArgs(string payload)
     {
         Payload = payload;
     }
 }

 public interface IHybridWebView : IView
 {
     occasion EventHandler<SourceChangedEventArgs> SourceChanged;
     occasion EventHandler<JavaScriptActionEventArgs> JavaScriptAction;
     occasion EventHandler<EvaluateJavaScriptAsyncRequest> RequestEvaluateJavaScript;

     void Refresh();

     Job EvaluateJavaScriptAsync(EvaluateJavaScriptAsyncRequest request);

     WebViewSource Supply { get; set; }

     void Cleanup();

     void InvokeAction(string information);

 }
     

 public class HybridWebView : View, IHybridWebView
 {
     public occasion EventHandler<SourceChangedEventArgs> SourceChanged;
     public occasion EventHandler<JavaScriptActionEventArgs> JavaScriptAction;
     public occasion EventHandler<EvaluateJavaScriptAsyncRequest> RequestEvaluateJavaScript;

     public HybridWebView()
     {

     }

     public async Job EvaluateJavaScriptAsync(EvaluateJavaScriptAsyncRequest request)
     {
         await Job.Run(() =>
         {
             RequestEvaluateJavaScript?.Invoke(this, request);
         });
     }

     public void Refresh()
     {
         if (Supply == null) return;
         var s = Supply;
         Supply = null;
         Supply = s;
     }

     public WebViewSource Supply
     {
         get { return (WebViewSource)GetValue(SourceProperty); }
         set { SetValue(SourceProperty, worth); }
     }

     public static readonly BindableProperty SourceProperty = BindableProperty.Create(
       propertyName: "Supply",
       returnType: typeof(WebViewSource),
       declaringType: typeof(HybridWebView),
       defaultValue: new UrlWebViewSource() { Url = "about:clean" },
       propertyChanged: OnSourceChanged);

     personal static void OnSourceChanged(BindableObject bindable, object oldValue, object newValue)
     {
         var view = bindable as HybridWebView;

         bindable.Dispatcher.Dispatch(() =>
         {
             view.SourceChanged?.Invoke(view, new SourceChangedEventArgs(newValue as WebViewSource));

         });
     }

     public void Cleanup()
     {
         JavaScriptAction = null;
     }

     public void InvokeAction(string information)
     {
         JavaScriptAction?.Invoke(this, new JavaScriptActionEventArgs(information));
     }
 }

and ios

utilizing CoreGraphics;

utilizing Basis;

utilizing Microsoft.Maui.Handlers; utilizing Microsoft.Maui.Platform;

utilizing WebKit;

utilizing WebViewHostExample.Controls;



 namespace WebViewHostExample.Platforms.iOS.Renderers { 

public class HybridWebViewHandler : ViewHandler<IHybridWebView, WKWebView> { public static PropertyMapper<IHybridWebView, HybridWebViewHandler> HybridWebViewMapper = new PropertyMapper<IHybridWebView, HybridWebViewHandler>(ViewHandler.ViewMapper);

const string JavaScriptFunction = "operate invokeCSharpAction(information){window.webkit.messageHandlers.invokeAction.postMessage(information);}";

    personal WKUserContentController userController;
    personal JSBridge jsBridgeHandler;
    static SynchronizationContext sync;


    public HybridWebViewHandler() : base(HybridWebViewMapper)
    {
        sync = SynchronizationContext.Present;
    }

    personal void VirtualView_SourceChanged(object sender, SourceChangedEventArgs e)
    {
        LoadSource(e.Supply, PlatformView);
    }

    protected override WKWebView CreatePlatformView()
    {
        sync = sync ?? SynchronizationContext.Present;
        jsBridgeHandler = new JSBridge(this);
        userController = new WKUserContentController();

        var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false);

        userController.AddUserScript(script);
        userController.AddScriptMessageHandler(jsBridgeHandler, "invokeAction");

        var config = new WKWebViewConfiguration { UserContentController = userController };
        var webView = new WKWebView(CGRect.Empty, config);

        return webView;            
    }

    protected override void ConnectHandler(WKWebView platformView)
    {
        base.ConnectHandler(platformView);

        if (VirtualView.Supply != null)
        {
            LoadSource(VirtualView.Supply, PlatformView);
        }

        VirtualView.SourceChanged += VirtualView_SourceChanged;
        VirtualView.RequestEvaluateJavaScript += VirtualView_RequestEvaluateJavaScript;
    }

    personal void VirtualView_RequestEvaluateJavaScript(object sender, EvaluateJavaScriptAsyncRequest e)
    {
        sync.Publish((o) =>
        {
            PlatformView.EvaluateJavaScript(e);
        }, null);
    }

    protected override void DisconnectHandler(WKWebView platformView)
    {
        base.DisconnectHandler(platformView);

        VirtualView.SourceChanged -= VirtualView_SourceChanged;

        userController.RemoveAllUserScripts();
        userController.RemoveScriptMessageHandler("invokeAction");
    
        jsBridgeHandler?.Dispose();
        jsBridgeHandler = null;
    }


    personal static void LoadSource(WebViewSource supply, WKWebView management)
    {
        if (supply is HtmlWebViewSource html)
        {
            management.LoadHtmlString(html.Html, new NSUrl(html.BaseUrl ?? "http://localhost", true));

        }
        else if (supply is UrlWebViewSource url)
        {
            management.LoadRequest(new NSUrlRequest(new NSUrl(url.Url)));
        }

    }

}

public class JSBridge : NSObject, IWKScriptMessageHandler
{
    readonly WeakReference<HybridWebViewHandler> hybridWebViewRenderer;

    inside JSBridge(HybridWebViewHandler hybridRenderer)
    {
        hybridWebViewRenderer = new WeakReference<HybridWebViewHandler>(hybridRenderer);
    }

    public void DidReceiveScriptMessage(WKUserContentController userContentController, WKScriptMessage message)
    {
        HybridWebViewHandler hybridRenderer;

        if (hybridWebViewRenderer.TryGetTarget(out hybridRenderer))
        {
            hybridRenderer.VirtualView?.InvokeAction(message.Physique.ToString());
        }
    }
}}

if i take advantage of the code like this:

    
 MainPageViewModel vm;

 public MainPage()
 {
     InitializeComponent();

     vm = new MainPageViewModel();
     MyWebView.BindingContext = vm;

     MyWebView.JavaScriptAction += MyWebView_JavaScriptAction;
 }

  protected override void OnParentSet()
    {
        base.OnParentSet();
        vm.Supply = new HtmlWebViewSource()
        {


,            Html = htmlSource
             BaseUrl = "ios path i would like????"
  
        };

   }

if i did this in android: and add the trail: “file:///android_asset/” it should work on android web-view

  protected override void OnParentSet()
    {
        base.OnParentSet();
        vm.Supply = new HtmlWebViewSource()
        {


,            Html = htmlSource
             BaseUrl = "file:///android_asset/"
  
        };

   }

it should work however i would like the identical factor in ios ???

what’s the default path that can go to “Assets/Uncooked/” in ios code of maui.internet



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments