WKWebView:远程 url 访问本地文件 [英] WKWebView: Remote url accessing local files

查看:51
本文介绍了WKWebView:远程 url 访问本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Xamarin.iOS,但一切都可以轻松转换为 ObjC/Swift.

I'm using Xamarin.iOS but everything is easily translatable to ObjC / Swift.

我有一个 WKWebView,它加载一个远程 URL(html、js、css),然后加载应用程序中本地存储的 mp4 视频.问题是这些视频没有被加载.

I have a WKWebView which loads a remote URL (html, js, css) that will then load mp4 videos stored locally within the app. The problem is these videos are not being loaded.

该网站使用 HTML 5 视频标签,我正在通过 C# 与 Javascript 通信来设置本地网址.

The site uses HTML 5 video tags and I'm setting the local urls by communicating C# with Javascript.

视频路径是这样的:/var/mobile/Containers/Data/Application/F535FA1C-752B-4B46-B237-6781464EE0E5/Documents/../Library/Vimeo/7b2723fa-b0f1-40c5-bc58-d43949273329.mp4.

这是我的 WKWebView 设置:

This is my WKWebView setup:

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...

// this.ViewModel.HtmlContent is a string which contains the html page
this.webView.LoadHtmlString(this.ViewModel.HtmlContent, NSUrl.FromString(Constants.BASE_URL));

我在 Info.plist 文件中配置了 ATS 例外.

I have configured ATS exceptions in the Info.plist file.

我尝试加载这个简单的 html 字符串:

I tried loading this simple html string:

var videoToPlay = "file:///var/mobile/Containers/Data/Application/F535FA1C-752B-4B46-B237-6781464EE0E5/Documents/../Library/Vimeo/7b2723fa-b0f1-40c5-bc58-d43949273329.mp4";
var html = $"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body  id=\"page\"><div id=\"VideoBean\" style=\"position:relative;\" width=\"305\" height=\"242\"><video id=\"video\" type=\"video/mp4\" controls=\"controls\" role=\"presentation\" width=\"305\" height=\"242\"><source id=\"source\" src=\"{videoToPlay}\"></video></div></body></html>";

this.webView.LoadHtmlString(html, NSUrl.FromString(Constants.BASE_URL));

结果是一样的.视频无法播放,我可以在 Safari 输出中看到此错误:

And the result is the same. The video does not play and I can see this error on the Safari output:

不允许加载本地资源:file:///var/mobile/Containers/Data/Application/F535FA1C-752B-4B46-B237-6781464EE0E5/Documents/../Library/Vimeo/7b2723fa-b0f1-40c5-bc58-d43949273329.mp4

Not allowed to load local resource: file:///var/mobile/Containers/Data/Application/F535FA1C-752B-4B46-B237-6781464EE0E5/Documents/../Library/Vimeo/7b2723fa-b0f1-40c5-bc58-d43949273329.mp4

第二次更新

如果我加载将本地路径设置为基本 url 的 html 片段,它会起作用:

2nd Update

If I load the html snippet setting the local path as base url, it works:

this.webView.LoadHtmlString(html, NSUrl.FromFilename(videoToPlay));

但是当我尝试加载远程站点时,我需要将基本 url 设为远程基本 url.所以一个新问题是:是否可以在 WKWebView 上设置两个基本网址?

But as I'm trying to load a remote site, I need the base url to be the remote base url. So a new question would be: is it possible to set two base urls on WKWebView?

否则,除了在本地加载整个网站之外,我还有其他选择吗?

Otherwise, do I have any option other than loading the entire site locally?

推荐答案

这是我所做的工作:

1) 将html文件下载到本地存储

1) Download the html file into the local storage

2) 在 html 文件中查找所有相对 url 并在其上设置基本 url.我的意思是这样的:src="myvideo.mp4 --> src="https://myhomeurl.com/myvideo.mp4"

2) Look for all relative urls in the html file and set the base url on them. I mean something like this: src="myvideo.mp4 --> src="https://myhomeurl.com/myvideo.mp4"

3) 使用 webView.LoadFileUrl 而不是 LoadHtmlString(我还将第二个参数从 NSUrl.FromString 更改为 NSUrl.FromFilename).

3) Use webView.LoadFileUrl instead of LoadHtmlString (I also changed the second parameter from NSUrl.FromString to NSUrl.FromFilename).

这篇关于WKWebView:远程 url 访问本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆