Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.IO;
- using CoreGraphics;
- using Foundation;
- using UIKit;
- using WebKit;
- using wellPlayed.Controls;
- using wellPlayed.iOS.Renderers;
- using Xamarin.Forms;
- using Xamarin.Forms.Platform.iOS;
- [assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))]
- namespace wellPlayed.iOS.Renderers
- {
- public class HybridWebViewRenderer : ViewRenderer<HybridWebView, WKWebView>
- {
- WKWebView wkWebView;
- protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
- {
- base.OnElementChanged(e);
- if (Control == null)
- {
- WKPreferences wkPreferences = new WKPreferences
- {
- JavaScriptEnabled = true,
- JavaScriptCanOpenWindowsAutomatically = true,
- };
- wkPreferences.SetValueForKey(NSObject.FromObject(true), (NSString)"allowFileAccessFromFileURLs");
- WKWebViewConfiguration configuration = new WKWebViewConfiguration()
- {
- AllowsInlineMediaPlayback = true,
- };
- configuration.Preferences = wkPreferences;
- wkWebView = new WKWebView(CGRect.Empty, configuration);
- SetNativeControl(wkWebView);
- }
- if (e.NewElement != null)
- {
- Control.BackgroundColor = UIColor.Green;
- }
- }
- protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- }
- public override void Draw(CGRect rect)
- {
- base.Draw(rect);
- Control.Frame = rect;
- }
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- using System;
- using System.IO;
- using Foundation;
- using ObjCRuntime;
- using UIKit;
- using WebKit;
- using wellPlayed.Controls;
- using wellPlayed.iOS.Renderers;
- using Xamarin.Forms;
- using Xamarin.Forms.Platform.iOS;
- [assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))]
- namespace wellPlayed.iOS.Renderers
- {
- public class HybridWebViewRenderer : ViewRenderer<WebView, WKWebView>
- {
- WKWebView wkWebView;
- protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
- {
- base.OnElementChanged(e);
- if (Control == null)
- {
- var config = new WKWebViewConfiguration();
- config.AllowsInlineMediaPlayback = true;
- config.AllowsAirPlayForMediaPlayback = true;
- config.AllowsPictureInPictureMediaPlayback = true;
- config.MediaPlaybackAllowsAirPlay = true;
- config.MediaPlaybackRequiresUserAction = false;
- config.RequiresUserActionForMediaPlayback = false;
- config.MediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypes.None;
- wkWebView = new WKWebView(Frame, config);
- wkWebView.NavigationDelegate = new WkWebviewPolicy();
- SetNativeControl(wkWebView);
- }
- }
- public class WkWebviewPolicy : WKNavigationDelegate
- {
- public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
- {
- base.DecidePolicy(webView, navigationAction, decisionHandler);
- }
- public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
- {
- base.DidFinishNavigation(webView, navigation);
- var JSstr = @"var videos = document.querySelectorAll('video'); for (var i = videos.length - 1; i >= 0; i--) { var ivideo = videos[i]; ivideo.setAttribute('webkit-playsinline','\'); ivideo.play(); };";
- webView.EvaluateJavaScript(JSstr, null);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment