我正在使用Xamarin.iOS,但一切都很容易翻译成ObjC/Swift.
我有一个WKWebView加载远程URL(html,js,css),然后加载应用程序中本地存储的mp4视频.问题是这些视频没有被加载.
该网站使用HTML 5视频标签,我通过与Javascript通信C#来设置本地网址.
视频路径是这样的:/var/mobile/Containers/Data/Application/F535FA1C-752B-4B46-B237-6781464EE0E5/Documents/../Library/Vimeo/7b2723fa-b0f1-40c5-bc58-d43949273329.mp4.
这是我的WKWebView设置:
var userController = new WKUserContentController();
userController.AddScriptMessageHandler(new iOSSiteWrapper(this.ViewModel, () => this.webView), "iOSWrapper");
var configuration = new WKWebViewConfiguration
{
AllowsInlineMediaPlayback = true,
UserContentController = userController
};
configuration.Preferences.JavaScriptCanOpenWindowsAutomatically = true;
configuration.Preferences.JavaScriptEnabled = true;
configuration.Preferences.SetValueForKey(NSObject.FromObject(true), new NSString("allowFileAccessFromFileURLs"));
configuration.AllowsInlineMediaPlayback = true;
configuration.AllowsPictureInPictureMediaPlayback = true;
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
configuration.MediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypes.None;
}
else
{
configuration.MediaPlaybackRequiresUserAction = false;
}
this.webView = new WKWebView(
new CoreGraphics.CGRect(),
configuration)
{
WeakNavigationDelegate = this
};
// add to view hierarchy... …Run Code Online (Sandbox Code Playgroud) 我们正在实现一个应用程序,我们在Javascript和c#之间进行通信.我们的UIWebView有一个按钮来调用一些本机功能.在UIWebView上,我在ShouldStartLoad上有一个处理程序.
webView.ShouldStartLoad = myHandler;
bool myHandler (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navType)
{
}
Run Code Online (Sandbox Code Playgroud)
这会在每次页面加载时调用.实际上,我只想从WebView的事件中调用它,例如点击按钮.从Javascript我有
window.location.href = "myapp://action?par1=abc&par2=def";
Run Code Online (Sandbox Code Playgroud)
如何从自定义URL调用特定函数?
从c#调用JavaScript
我试图从c#回调JavaScript,但它没有调用TestShow()函数
wkWebView.EvaluateJavaScript(string.Format("TestShow()"), (r, e) =>
{
Console.WriteLine("In EvaluateJavaScript");
if (e != null) Console.WriteLine(e);
});
Run Code Online (Sandbox Code Playgroud)
JavaScript方面我有一个警报,但它没有显示警报
function TestShow()
{
alert("Hello! I am an alert box!!");
}
Run Code Online (Sandbox Code Playgroud)