I use Xamarin.iOS, but everything translates easily to ObjC / Swift.
I have a WKWebView that loads a remote URL (html, js, css), which then loads mp4 videos stored locally in the application. The problem is that these videos are not loading.
The site uses HTML 5 video tags, and I set local URLs by linking C # to Javascript.
The video tracks look something like this: /var/mobile/Containers/Data/Application/F535FA1C-752B-4B46-B237-6781464EE0E5/Documents/../Library/Vimeo/7b2723fa-b0f1-40c5-bc58-d43949273329.mp4 .
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));
I configured ATS exceptions in the Info.plist file.
Update
I tried loading this simple html line:
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));
And the result is the same. The video does not play, and I see this error on Safari output:
It is not allowed to load a local resource: file: ///var/mobile/Containers/Data/Application/F535FA1C-752B-4B46-B237-6781464EE0E5/Documents/../Library/Vimeo/7b2723fa-b0f1-40c5- bc58-d43949273329. mp4
Second update
If I load an html fragment specifying the local path as the base url, it works:
this.webView.LoadHtmlString(html, NSUrl.FromFilename(videoToPlay));
But since I am trying to load a remote site, I need a base url for the remote base url. So a new question: is it possible to set two base URLs on a WKWebView?
Otherwise, do I have any option other than loading the entire site locally?